孙肖宁

python计算当前时间的前(后)一分钟,前(后)一小时,前(后)一天
计算当前时间import datetime datetime.datetime.now() # 2019-06-3...
扫描右侧二维码阅读全文
30
2019/06

python计算当前时间的前(后)一分钟,前(后)一小时,前(后)一天

计算当前时间

import datetime
datetime.datetime.now()
# 2019-06-30 10:51:14.089271
# 格式化时间
datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# 2019-06-30 10:52:05

多加(减)一天

(datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%
d %H:%M:%S")
# 2019-07-01 10:54:44
(datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%
d %H:%M:%S")
# 2019-06-29 10:55:44

多加(减)一小时

(datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
# 2019-06-30 11:56:46
(datetime.datetime.now()-datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
# 2019-06-30 09:58:02

多加(减)一分钟

(datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")

(datetime.datetime.now()-datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")
Last modification:June 30th, 2019 at 11:00 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment