python 当前时间多加一天、一小时、一分钟

python 当前时间多加一天、一小时、一分钟

首先看下,datetime的使用

import datetime

>>> print datetime.datetime.now()
2017-07-15 15:01:24.619000
  • 1
  • 2
  • 3
  • 4

格式化时间

>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 2017-07-15 15:01:35 >>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M") 2017-07-15 15:01 >>> print datetime.datetime.now().strftime("%Y%m%d") 20170715
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

多加一天

>>> print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-% d %H:%M:%S") 2017-07-16 15:12:42 >>>
  • 1
  • 2
  • 3
  • 4

多加一小时

>>> print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m- %d %H:%M:%S") 2017-07-15 16:10:43 >>>
  • 1
  • 2
  • 3
  • 4

多加一分钟

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%
m-%d %H:%M:%S") 2017-07-15 15:12:56 >>>

猜你喜欢

转载自www.cnblogs.com/fengff/p/9285057.html