当前位置 : 主页 > 编程语言 > python >

python获取前几个月时间

来源:互联网 收集:自由互联 发布时间:2022-08-10
#!/usr/bin/env pythpn import datetime now = datetime.datetime.now() date_now = now.date() def get_date_month(mon=0): last_m = (int(now.year) * 12 + int(now.month) - mon) % 12 if len(str(last_m)) == 1: last_m = "%02d" % last_m last_y = int((
#!/usr/bin/env pythpn
import datetime
now = datetime.datetime.now()
date_now = now.date()


def get_date_month(mon=0):
last_m = (int(now.year) * 12 + int(now.month) - mon) % 12
if len(str(last_m)) == 1:
last_m = "%02d" % last_m
last_y = int((int(now.year) * 12 + int(now.month) - mon) / 12)
last_mon = '%s%s' % (last_y, last_m)

return last_mon

last_mon = get_date_month(4);
print(last_mon)

$python get_year_month.py
202203


上一篇:python 嵌套列表的列表的字符长度排序
下一篇:没有了
网友评论