import datetime import time # 导入日历模块 import calendar import os # 获取当时的时间,为2022年5月5日 10:00:00 today = datetime.datetime.today() # 将日期替换为当前日期坐在月份的1日,即2022年5月1日0时0分0秒 first = today.replace(day=1, hour=0, minute=0, second=0, microsecond=0) print(first) # 获得上个月的最后一天的日期 last_month = first - datetime.timedelta(days=1) print(last_month.date()) # 获取2022年5月份的天数,结果可用.days获取天数,可用.seconds获取秒数 len_months = first.replace(month=(first.month + 1)) - first print(len_months.days) # 获得日期 for ii in range(40): print(first + datetime.timedelta(days=ii)) # 获取2021年一年有多少天 next_year = first.replace(year=2022, month=1, day=1, hour=0, minute=0, second=0, microsecond=0) - \ first.replace(year=2021, month=1, day=1, hour=0, minute=0, second=0, microsecond=0) print(next_year) # 使用time模块,将时间换为某个格式(如:('%Y-%m-%d'))的字符串 print(first.strftime('%Y-%m-%d')) # 使用time模块,将字符串换个时间 print(time.strptime('2022-02-02 10:00:00', '%Y-%m-%d %H:%M:%S')) # 判断平年还是闰年 print(calendar.isleap(first.year))
08-26
617
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交