- import datetime
- # 获取当前时间
- print(datetime.datetime.now()) # 2017-07-15 15:01:24.619000
- # 格式化时间
- 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")) #
- # 加一天
- print((datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")) # 2017-07-16 15:12:42
- # 加一小时
- print((datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")) # 2017-07-15 16:10:43
- # 加一分钟
- print((datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")) # 2017-07-15 15:12:56