• time时间模块


    time时间模块的常用方法:

    sleep时间延迟

     1 time.sleep(5) # 程序走到这儿会等待5秒钟 

    时间的格式:

    1 # '2018-8-20' '2018.8.20' 字符串数据类型     格式化时间 - 给人看的
    2 # 结构化时间
    3 # 1574275685.803445  浮点型数据类型,以s秒为单位 时间戳时间 - 给机器计算用的
    4 # 1970 1 1 0:0:0

    时间戳时间:

    print(time.time())
    #结果:1574275685.803445

    strftime格式化时间:

    1 print(time.strftime('%Y-%m-%d %H:%M:%S')) #字符串类型时间
    2 #结果:2019-11-21 02:49:25
    3 print(time.strftime('%y-%m-%d %H:%M:%S')) #字符串类型时间
    4 #结果:19-11-21 02:49:25
    5 print(time.strftime('%c'))
    6 #结果:Thu Nov 21 02:49:25 2019

    localtime结构化时间:

    1 struct_time = time.localtime()  # 北京时间
    2 print(struct_time)
    3 #结果:time.struct_time(tm_year=2019, tm_mon=11, tm_mday=21, tm_hour=2, tm_min=53, tm_sec=31, tm_wday=3, tm_yday=325, tm_isdst=0)
    4 print(struct_time.tm_mon)#输出月份的意思
    5 #结果:11

    时间戳换成字符串时间:

    1 print(time.time())  #输出时间戳
    2 #结果:1574276286.912888
    3 struct_time = time.localtime(1574276286)    #结构化时间
    4 print(time.gmtime(1574276286))
    5 #结果:time.struct_time(tm_year=2019, tm_mon=11, tm_mday=20, tm_hour=18, tm_min=58, tm_sec=6, tm_wday=2, tm_yday=324, tm_isdst=0)
    6 ret = time.strftime('%y-%m-%d %H:%M:%S',struct_time)#里面的参数为输出的格式
    7 print(ret)
    8 #结果:19-11-21 02:58:06

    字符串时间 转 时间戳:

    1 struct_time = time.strptime('2019-11-21','%Y-%m-%d')
    2 print(struct_time)
    3 #结果:time.struct_time(tm_year=2019, tm_mon=11, tm_mday=21, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=325, tm_isdst=-1)
    4 res = time.mktime(struct_time)
    5 print(res)
    6 #结果:1574265600.0

    #练习:
    # 1.查看一下2000000000时间戳时间表示的年月日
    # 2.将2018-8-20转换成时间戳时间
    # 3.请将当前时间的当前月1号的时间戳时间取出来 - 函数
    # 4.计算时间差 - 函数

  • 相关阅读:
    101. Symmetric Tree(js)
    100. Same Tree(js)
    99. Recover Binary Search Tree(js)
    98. Validate Binary Search Tree(js)
    97. Interleaving String(js)
    96. Unique Binary Search Trees(js)
    95. Unique Binary Search Trees II(js)
    94. Binary Tree Inorder Traversal(js)
    93. Restore IP Addresses(js)
    92. Reverse Linked List II(js)
  • 原文地址:https://www.cnblogs.com/yjtxin/p/11905739.html
Copyright © 2020-2023  润新知