Python计算上个月最后一天和第一天

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/QuantumEnergy/article/details/78090123
import time
from datetime import datetime
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, days).strftime('%Y-%m-%d')


def getFirstDayOfLastMonth():
    d = datetime.now()
    c = calendar.Calendar()
    year = d.year
    month = d.month
    if month == 1:
        month = 12
        year -= 1
    else:
        month -= 1
    return datetime(year, month, 1).strftime('%Y-%m-%d')
print getFirstDayOfLastMonth()
print getLastDayOfLastMonth()

没有更多推荐了,返回首页