> import time import datetime
1.时间字符串和时间戳转换
dateStr = "2019-5-10 23:40:00"
timeArray = time.strptime(dateStr, "%Y-%m-%d %H:%M:%S")
将时间元组转换为时间戳:
timeStamp = int(time.mktime(timeArray))
格式化时间戳为时间元组
timeArray = time.localtime(timeStamp)
将时间元组转换为时间字符串
timeStr = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
2.时间对象和字符串转换
now = datetime.datetime.now()
time = now.strftime('%Y-%m-%d %H:%M:%S')
date = '2021-05-12'
dt = datetime.datetime.strptime(date, '%Y-%m-%d %H:%M:%S')
3.字符串和时间对象转换
t_stamp = time.time()
t_datetime = datetime.datetime.fromtimestamp(int(t_stamp))
t_datetime.timestamp()
4.增加时间
(datetime.datetime.now()+datetime.timedelta(seconds=30)).strftime("%Y-%m-%d %H:%M:%S")