from datetime import datetime,timedelta,timezone #获取当前时间 now=datetime.now() print(now) #构造指定的时间2007-12-31 11:30:12 dt=datetime(2007,12,31,11,30,12) print(dt) #用给定时间换算时间戳 print(dt.timestamp()) #将一个时间戳换算为本地个时间 t=datetime.fromtimestamp(1119071000.0) t1=datetime.utcfromtimestamp(1119071000.0) print(t) print(t1) #将用户输入的字符串时间转换为datetime cday=datetime.strptime(‘2017-6-1 12:30:01‘,‘%Y-%m-%d %H:%M:%S‘) print(cday,type(cday)) #使用strftime将时间转换为字符串,根据需要调整显示格式 #格式1‘Fri, Apr 06 13:43:43‘格式2‘2018, 04 06 13:42:13‘ now=datetime.now() print(now.strftime("(‘%a, %b %d %H:%M:%M‘")) print(now.strftime("(‘%Y, %m %d %H:%M:%S‘")) #时间的加减运算 now=datetime.now() old_now=datetime(2007,12,31,11,30,12,1000) p=now-old_now #获取差了多少天 print(p.days) new_time=now + timedelta(days=1,hours=12) #print(new_time) #设定本地时区时间 a=timezone(timedelta(hours=8)) print(a)#UTC+08:00