Python时间小插件-上个月初月末-本月初本月末等
涵盖了上个月初、上个月末、本月初、本月末、区间所有时间
import datetime
import calendar
# 上个月初
def last_time():
data = (datetime.date.today().replace(day=1) - datetime.timedelta(1)).replace(day=1)
return datetime.datetime.strptime(str(data), "%Y-%m-%d").strftime("%Y.%m.%d")
# 上个月末
def last_last():
data = datetime.date(datetime.date.today().year,datetime.date.today().month,1)-datetime.timedelta(1)
return datetime.datetime.strptime(str(data), "%Y-%m-%d").strftime("%Y.%m.%d")
# 本月月初
def now_one():
data = datetime.date.today().replace(day=1)
return datetime.datetime.strptime(str(data), "%Y-%m-%d").strftime("%Y.%m.%d")
# 本月月末
def now_last():
w,d = calendar.monthrange(datetime.date.today().year,datetime.date.today().month)
data = datetime.date(datetime.date.today().year,datetime.date.today().month,d)
return datetime.datetime.strptime(str(data), "%Y-%m-%d").strftime("%Y.%m.%d")
# 输入开始,结束时间返回中间所有时间
def time_list(data,data2):
starttime = datetime.datetime.strptime(data, "%Y%m%d")
dateend = datetime.datetime.strptime(data2, "%Y%m%d")
detal = datetime.timedelta(days=1)
isstart = True
daylist = []
while starttime < dateend:
if isstart:
starttime = starttime
isstart = False
else:
starttime += detal
daylist.append("%s号" % starttime.day)
return daylist