• python的time几种用法strptime、strftime、localtime、mktime


    1、time.time()

    import time
    t1 = time.time()
    print(t1)  # 1594006474.1072185

    2、获取当地时间-->结构化时间   time.localtime() 

    import time
    
    t2 = time.localtime() 
    
    print(t2)
     #  time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)

    3、根据指定格式,获取结构化时间   time.strptime()

    import time
    
     t2 = time.strptime("30 11 2000", "%d %m %Y")
    
    print(t2) 
      # time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1)
        %Y  Year with century as a decimal number.
        %m  Month as a decimal number [01,12].
        %d  Day of the month as a decimal number [01,31].
        %H  Hour (24-hour clock) as a decimal number [00,23].
        %M  Minute as a decimal number [00,59].
        %S  Second as a decimal number [00,61].
        %z  Time zone offset from UTC.
        %a  Locale's abbreviated weekday name.
        %A  Locale's full weekday name.
        %b  Locale's abbreviated month name.
        %B  Locale's full month name.
        %c  Locale's appropriate date and time representation.
        %I  Hour (12-hour clock) as a decimal number [01,12].
        %p  Locale's equivalent of either AM or PM.
    

      

    import time
     t2 = time.strptime("2020-07-01", "%Y{y}%m{y}%d".format(y="-"))
    
    print(t2) 
    # time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)

    4、根据结构时间,获取指定格式时间  time.strftime()

    import time
    
    t2 = time.localtime()  #  time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
    
    
    t3 = time.strftime("%Y{y}%m{y}%d".format(y="-"),t2)
    print(t3) 
     # 2020-07-01

    5、根据结构时间,获取时间戳   time.mktime()

    import time
    
    t2 = time.localtime()  #  time.struct_time(tm_year=2020, tm_mon=7, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=183, tm_isdst=-1)
    
    
    t4 = time.mktime(t2)
    print(t4)
    # 1593532800.0

    6、补充datetime用法。

    相关链接:https://www.cnblogs.com/pywjh/p/9526094.html

    7、date用法

    from datetime import date,timedelta
    
    date_today = date.today()
    
    print(date_today)  # 2020-07-11
    from datetime import date,timedelta
    
    # date_today = date.today()
    
    # print(date_today)  # 2020-07-11
    
    t1 = timedelta(days=7)
    
    print(t1)  # 7 days, 0:00:00
    from datetime import date,timedelta,datetime
    
    # date_today = date.today()
    
    # print(date_today)  # 2020-07-11
    
    # t1 = timedelta(days=7)
    #
    # print(t1)  # 7 days, 0:00:00
    
    
    t2 = datetime.strptime("2020-07-01", '%Y-%m-%d')
    
    print(t2)  # 2020-07-01 00:00:00

    最后出现神奇的一幕

    from datetime import date,timedelta,datetime
    
    # date_today = date.today()
    
    # print(date_today)  # 2020-07-11
    
    # t1 = timedelta(days=7)
    #
    # print(t1)  # 7 days, 0:00:00
    
    t2 = datetime.strptime("2020-07-01", '%Y-%m-%d')
    #
    # print(t2)  # 2020-07-01 00:00:00
    
    t3 = timedelta(days=7)
    
    print(t3)  #  7 days, 0:00:00
    
    t4 = t2 - t3
    
    print(t4)  # 2020-06-24 00:00:00
  • 相关阅读:
    加密的PDF文件怎么查看
    SQL Server数据库中怎么加密数据
    虚拟磁盘也加密
    怎么用密码保护您的文件夹
    如何用cmd命令加密文件夹
    快压轻松创建加密压缩包
    GPS学习的一些链接
    【最新软件预告】邮件群发器Console版
    第六格很酷的滚动活动会员条
    邀请您加入移动开发专家联盟
  • 原文地址:https://www.cnblogs.com/youhongliang/p/13254560.html
Copyright © 2020-2023  润新知