Home >Backend Development >Python Tutorial >Python gets the date of the last day of the previous month

Python gets the date of the last day of the previous month

大家讲道理
大家讲道理Original
2016-11-09 10:49:353457browse

[Python] Code

from datetime import datetime
from datetime import timedelta
import calendar
def getLastDayOfLastMonth():
    d = datetime.now()
    c = calendar.Calendar()
     
    year = d.year
    month = d.month
     
    if month == 1 :
        month = 12
        year -= 1
    else :
        month -= 1
    days = calendar.monthrange(year, month)[1]   
    return (datetime(year,month,1)+timedelta(days= days)).strftime('%Y-%m-%d %X')


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:python bubble sortNext article:python bubble sort