import datetime
def which_month(last=None):
# last=None 求上个月是哪年哪个月, last!=None 是上上个月
if last is None:
last_month = yesterday()[:-2]
else:
month = datetime.datetime.strptime(yesterday(), "%Y%m%d").month
if month == 1:
year = datetime.datetime.strptime(yesterday(), "%Y%m%d").year
year = year - 1
month = 12
last_month = str(year) + str(month)
else:
last_month = str(int(yesterday()[:-2]) - 1)
return last_month
def current_date():
return datetime.datetime.now().strftime('%Y%m%d')
def today():
return datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')
def yesterday(days=1):
delta = datetime.timedelta(days=days)
today_s = datetime.datetime.strptime(current_date(), "%Y%m%d")
yesterday = (today_s - delta).strftime("%Y%m%d")
return yesterday
```
python 判断上个月是哪个月
最新推荐文章于 2024-07-10 09:09:09 发布