时间戳
timestamp = time.time()
print(timestamp)
时间元组
timetuple = time.localtime()
print(timetuple)
本地时间
timelocal = time.asctime(time.localtime(time.time()))
print(timelocal)
格式化时间
time_str = time.strftime("%Y-%m-%d %H:%M:%S")
print(time_str, type(time_str))
time_str = time.strftime("%a %b %d %H:%M:%S %Y")
time_str = time.strftime("%A %B %D %H:%M:%S %Y")
当天日期
today = datetime.date.today()
print(today, type(today))
前一天日期
today = datetime.datetime.now()
yesterday = (today - datetime.timedelta(days=1)).strftime('%Y%m%d')
print(today, type(today))
print(yesterday, type(yesterday))
上周第一天和最后一天
today = datetime.datetime.now()
last_week_start = today - datetime.timedelta(days=today.weekday() + 7)
last_week_end = today - datetime.timedelta(days=today.weekday() + 1)
print(last_week_start, last_week_end)
某年某月日历
import calender
cal = calendar.month(2021, 3)
print(cal, type(cal))
某年某月日历列表
cal_list = calendar.monthcalendar(2021, 3)
print(cal_list)