Python3时间转换及计算

import time, datetime

1. 把一个字符串转换成结构化的时间

t1 = time.strptime("2018-09-28 18:32:13", "%Y-%m-%d %H:%M:%S")
print(t1)
# time.struct_time(tm_year=2018, tm_mon=9, tm_mday=28, tm_hour=18, tm_min=32, tm_sec=13, tm_wday=4, tm_yday=271, tm_isdst=-1)

2. 把结构化的时间转换成字符串

t2 = time.localtime()
t3 = time.strftime("%Y.%m.%d %H:%M:%S", t2)
print(t3)  # 2019.08.03 16:03:49

3. 时间的计算

t4 = datetime.datetime.now()
print(t4)  # 2019-08-03 15:57:37.114858

# 在t4的基础上加上5天
t5 = t4 + datetime.timedelta(days=5)
print(t5)  # 2019-08-08 15:57:37.114858

转载于:https://my.oschina.net/zerobit/blog/3083049

你可能感兴趣的:(Python3时间转换及计算)