当前位置:Gxlcms > Python > Python获取上一个月最后一天的日期

Python获取上一个月最后一天的日期

时间:2021-07-01 10:21:17 帮助过:450人阅读

[Python]代码

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')

人气教程排行