datetime => string
now = datetime.datetime.now()
now.strftime('%Y-%m-%d %H:%M:%S')
string => datetime
t_str = '2019-07-05 16:26:23'
d = datetime.datetime.strptime(t_str, '%Y-%m-%d %H:%M:%S')
from datetime import datetime, timedelta
# 将字符串转换为时间
time_str = "2019-03-12 12:00:00"
d1 = datetime.strptime(time_str, '%Y-%m-%d %H:%M:%S')
# 将d1时间加8天
d2 = d1 + timedelta(days=8)
d1 = datetime.datetime.strptime('2019-07-18 17:41:20', '%Y-%m-%d %H:%M:%S')
d2 = datetime.datetime.strptime('2019-07-15 17:41:20', '%Y-%m-%d %H:%M:%S')
delta = d1 - d2
print(delta.days) # 3
now = datetime.datetime.now()
delta = datetime.timedelta(days=3)
n_days = now + delta
print(n_days.strftime('%Y-%m-%d %H:%M:%S')) # 2019-07-13 17:44:50
参数 | 类型 |
---|---|
weeks | 周 |
days | 天 |
hours | 小时 |
minutes | 分钟 |
seconds | 秒 |
microseconds | 微秒 |
milliseconds | 毫秒 |