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

[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’)

Posted in 未分类

发表评论