代码即注释,获取当前时间,当前时间上个月,当前时间下个月,当前时间上一周下一周,字符串转换时间戳
获取当年每个月第一天,最后一天,api不清楚ilde直接查一下或者百度即可,非常简单.
import datetime
import time
import calendar
import calendar as cal
now_day = datetime.datetime.now()
need_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
need_time = datetime.datetime.now().strftime('%Y-%m-%d')
pre_week = (datetime.datetime.now() + datetime.timedelta(days=-7)).strftime('%Y-%m-%d')
next_week = (datetime.datetime.now() + datetime.timedelta(days=7)).strftime('%Y-%m-%d')
pre_month = (datetime.datetime.now() + datetime.timedelta(days=-30)).strftime('%Y-%m-%d')
next_month = (datetime.datetime.now() + datetime.timedelta(days=-30)).strftime('%Y-%m-%d')
cur_week_first_day = (now_day - datetime.timedelta(days = now_day.weekday())).strftime('%Y-%m-%d')
cur_week_last_day = (now_day + datetime.timedelta(days = now_day.weekday())).strftime('%Y-%m-%d')
cur_month_first_day = datetime.datetime(now_day.year, now_day.month, 1).strftime('%Y-%m-%d')
cur_month_last_day = datetime.datetime(now_day.year, now_day.month, calendar.monthrange(now_day.year, now_day.month)[1]).strftime("%Y-%m-%d")
print('next week: ', next_week)
print('cur week: ', need_time)
print('pre week: ', pre_week)
print('cur week first day: ', cur_week_first_day)
print('cur week last day: ', cur_week_last_day)
print('cur month first day: ', cur_month_first_day)
print('cur month last day: ', cur_month_last_day)
print('need time', need_time)
print(type(need_time))
time_array = time.strptime(need_time, "%Y-%m-%d")
time_stamp = time.mktime(time_array)
print('time stamp: ', time_stamp)
print(type(time_stamp))
teme_format = "%d-%d-%d %d-%d-%d"
year = 2021
for m in range(1, 13):
d = cal.monthrange(year, m)
print(teme_format % (year, m, 1, year, m, d[1]))
输出: