time模块和datetime模块

时间分为三种模式(time 模块)

  1. 时间戳   (time.time())

  2. 格式化字符串 (time.strftime(%Y-%m-%d %H:%M:%S %p))

  3. 结构化的时间对象  (time.localtime()  time.gmtime())

    import time
    print(time.localtime())
    print(time.localtime().tm_hour)
    print(time.localtime().tm_wday)
    print(time.localtime().tm_yday)
    print(time.gmtime())
    #时间转换
    #时间戳---->struct_time------->格式化的字符串
    import time
    struct_time=time.localtime(123123)
    print(struct_time)
    ​
    print(time.strftime('%Y-%m-%d',struct_time))
    ​
    #格式化的字符串---->struct_time------->时间戳
    struct_time=time.strptime('2017-03-11','%Y-%m-%d')
    print(struct_time)
    ​
    print(time.mktime(struct_time))
    ​
    ​
    import datetime   #datetime模块
    print(datetime.datetime.now())
    ​
    print(datetime.datetime.fromtimestamp(1231231))
    ​
    print(datetime.datetime.now() + datetime.timedelta(days=3))
    print(datetime.datetime.now() - datetime.timedelta(days=3))
    print(datetime.datetime.now() + datetime.timedelta(days=-3))
    print(datetime.datetime.now() + datetime.timedelta(days=3,hours=3))

     

posted on 2018-12-05 19:08  小锦毛  阅读(148)  评论(0编辑  收藏  举报