import datetime import time astime = '2019-07-17 08:21:05' # 求 astime 5h后的时间 # 1.转成时间戳 2.datetime.datetime.fromtimestamp(int(时间戳)) + datetime.timedelta(hours=10) int时间戳因为不需要毫秒 astime_timestamp = time.mktime(time.strptime(astime,'%Y-%m-%d %H:%M:%S')) # 转成时间戳 print(astime_timestamp) #1563322865.0 res = datetime.datetime.fromtimestamp(int(astime_timestamp)) + datetime.timedelta(hours=10) #当前时间+10小时 print(res) #2019-07-17 18:21:05 print(type(res)) #<class 'datetime.datetime'> #要想变成对应字符串要str强转一下
判断是不是周几:
astime_timestamp = time.mktime(time.strptime('2019-08-04 08:21:05','%Y-%m-%d %H:%M:%S')) # 转成时间戳 res = datetime.datetime.fromtimestamp(int(astime_timestamp)) + datetime.timedelta(hours=10) #当前时间+10小时 print(res) #2019-07-17 18:21:05 print(res.weekday()+1) #7 周日 datetime对象.weekday() 返回周几