datetime模块

datetime模块

# datetime模块可以看成是时间加减的模块
import datetime
  1. 返回当前时间

    print(datetime.datetime.now())
    

    2019-08-16 19:33:39.557899

  2. 更改时间

    # 当前时间+3天
    print(datetime.datetime.now() + datetime.timedelta(3))
    

    2019-08-19 19:33:39.557899

    当前时间3小时
    print(datetime.datetime.now() + datetime.timedelta(hours=3))
    

    2019-08-19 22:33:39.557899

    # 当前时间+3分钟
    print(datetime.datetime.now() + datetime.timedelta(minutes=3))
    

    2019-08-19 22:33:42.557899

  3. 时间替换

    # 时间替换
    c_time = datetime.datetime.now()
    print(c_time.replace(minute=20, hour=5, second=13))
    

    2019-08-16 05:20:13.617117

posted @ 2019-08-24 11:20  SweetJack  阅读(64)  评论(0编辑  收藏  举报