# 导入time模块
import time
时间分为三种格式:
作用:用于时间间隔的计算
print(time.time()) # 1970年1月1日当前经过的秒数
作用:用于展示时间
print(time.strftime('%Y-%m-%d %H:%M:%S %p')) # 2020-03-30 21:16:28 PM
print(time.strftime('%Y-%m-%d %X')) # 2020-03-30 21:16:28
# %X 的作用等同于 %H:%M:%S
作用:用于单独获取时间的某一部分
print(time.localtime()) # time.struct_time(tm_year=2020, tm_mon=3, tm_mday=30, tm_hour=21, tm_min=21, tm_sec=32, tm_wday=0, tm_yday=90, tm_isdst=0)
print(res.tm_year) # 2020
print(res.tm_yday) # 90
import datetime
# print(datetime.datetime.now())
# print(datetime.datetime.now() + datetime.timedelta(days=3))
# print(datetime.datetime.now() + datetime.timedelta(weeks=1))
import time
s_time=time.localtime()
print(s_time)
print(time.mktime(s_time))
# 时间戳->struct_time
tp_time=time.time()
print(time.localtime(tp_time))
# 补充:世界标准时间与本地时间
print(time.localtime()) # 本地时间
print(time.gmtime()) # 世界标准时间
print(time.localtime(333333333))
print(time.gmtime(333333333))
s_time=time.localtime()
print((time.strftime('%Y-%m-%d %H:%M:%S',s_time)))
print(time.strptime('1988-03-03 11:11:11','%Y-%m-%d %H:%M:%S'))
真正需要掌握的:
format string—>struct_time—>timestamp
struct_time = time.strptime('1988-03-03 11:11:11', '%Y-%m-%d %H:%M:%S')
timestamp = time.mktime(struct_time)
print(timestamp)
format string<—struct_time<—timestamp
res = time.strftime('%Y-%m-%d %X', time.localtime(timestamp))
print(res)
# import time
# print(time.asctime())
import datetime
print(datetime.datetime.now())
print(datetime.datetime.utcnow())
print(datetime.datetime.fromtimestamp(333333))