• 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
  • 相关阅读:
    Linux之SSH详解
    Linux下的静态路由配置
    Linux之防火墙配置
    五、SQL 转换:Oracle 转 MySQL
    四、Oracle转Mysql总结
    三、MySQL 替代 Oracle 序列以及自增长处理
    二、MySQL 高级部分
    一、MySQL 基础
    15-1、常用类
    14-1、GUI
  • 原文地址:https://www.cnblogs.com/youhongliang/p/13254560.html
Copyright © 2020-2023  润新知