获取上月日期

# 获取上个月的时间
import
datetime today = datetime.date.today() # 鉴于月份只能在1-12中,月份为1月时,年份-1,月份+11,即为去年12月时间 if today.month == 1: last_month = today.replace(year=today.year - 1).replace(month=today.month + 11) last_month_date = last_month.strftime("%Y-%m-%d") print(last_month_date) else: # 除开1月,其他月份按照正常逻辑处理 last_month = today.replace(month=today.month + 1) last_month_date = last_month.strftime("%Y-%m-%d") print(last_month_date)
# 获取今日最大时间
a = datetime.datetime.combine(datetime.datetime.now(),datetime.time.max).strftime('%Y-%m-%d %H:%M:%S')
print(a)

# 获取今日最大时间
d = datetime.datetime.now()
t = datetime.time(23,59,59)
print(datetime.datetime.combine(d, t))
import datetime

# 上个月今日最小时间
print(datetime.datetime.combine(datetime.datetime.now() - datetime.timedelta(days=31),datetime.time(00,00,00)).strftime("%Y-%m-%d %H:%M:%S"))

# 今天最大时间
print(datetime.datetime.combine(datetime.datetime.now(),datetime.time(23,59,59)).strftime("%Y-%m-%d %H:%M:%S"))
自有风云来时雨, 似有风霜沾蓑衣
原文地址:https://www.cnblogs.com/meipu/p/14246542.html