主要涉及一些时间处理,格式输出,时间戳转换
time
import time
now = time.time() #获取当前时间戳,float
now_tuple = time.localtime(now)
print(now_tuple)
#格式化显示
now_str = time.strftime('%Y-%m-%d %H:%M:%S %p',now_tuple)
print(now_str)
now_str1 = time.strftime('%Y-%m-%d %H:%M:%S %p')
print(now_str1)
print(time.asctime(now_tuple))
#时间元组转化为时间戳
time_tuple = (2020,3,5,12,25,33,0,0,0)
print(time.mktime(time_tuple))
datetime
import datetime
#获取当前时间
print(datetime.datetime.today())
print(datetime.datetime.now())
#日期转时间戳
now_str = datetime.datetime.now()
now = now_str.timestamp()
print(now)
#时间戳转日期
time1 = 1676287632
t = datetime.datetime.fromtimestamp(time1) #时间戳转日期时间
print(t)
#自定义格式化展示时间
now_time = datetime.datetime.today()
now_str = now_time.strftime('%Y-%m-%d %H:%M:%S %p')
print(now_str)