Python 时间戳、时间字符串、时间对象以及时间加减处理

import time , datetime

时间字符串加8小时

timestring = “2019-12-21 10:22:56”

转为时间戳

timestamp = time.mktime(time.strptime(timestring, ‘%Y-%m-%d %H:%M:%S’))
print timestamp # 1576894976.0

datetime_struct = datetime.datetime.fromtimestamp(timestamp)
print type(datetime_struct) #

datetime_obj = (datetime_struct + datetime.timedelta(hours=8))
print datetime_obj # 2019-12-21 18:22:56

datetime_str = datetime_obj.strftime(‘%Y-%m-%d %H:%M:%S’)
print datetime_str # 2019-12-21 18:22:56

链接到文章: https://ixvps.com/46554.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注