• Python日期


    1. datatime

    from datetime import datetime, date
    
    now = datetime.now()
    print(now)  # 2020-01-20 01:24:01.843183
    
    to = date.today()
    print(to)  # 2020-01-20
    
    print(datetime(2000, 1, 1, 1, 0, 0))  # 2000-01-01 01:00:00
    
    delta = now - datetime(2000, 1, 1, 1, 0, 0)
    print(now, type(now))  # 2020-01-20 01:24:01.843183 <class 'datetime.datetime'>
    
    print(now.date())  # 2020-01-20
    
    print(now.time())  # 2020-01-20
    
    print(delta)  # 7324 days, 0:24:01.843183
    
    print(delta.days)  # 7324

    1.1 strptime

    from datetime import datetime
    S=datetime.strptime('2019/07/07','%Y/%m/%d')
    print(S,type(S))
    S=datetime.strptime('2019年7月7日星期日','%Y年%m月%d日星期日')
    print(S,type(S))
    S=datetime.strptime('2019年7月7日星期日8时42分24秒','%Y年%m月%d日星期日%H时%M分%S秒')
    print(S,type(S))
    S=datetime.strptime('7/7/2019','%m/%d/%Y')
    print(S,type(S))
    S=datetime.strptime('7/7/2019 8:42:50','%m/%d/%Y %H:%M:%S')
    print(S,type(S))
     
    #结果:
    2019-07-07 00:00:00 <class 'datetime.datetime'>
    2019-07-07 00:00:00 <class 'datetime.datetime'>
    2019-07-07 08:42:24 <class 'datetime.datetime'>
    2019-07-07 00:00:00 <class 'datetime.datetime'>
    2019-07-07 08:42:50 <class 'datetime.datetime'> 
    

    1.2 strftime

    dt=datetime.now()
    s=dt.strftime('%m/%d/%Y %H:%M:%S %p')
    print(s,type(s))
    s=dt.strftime('%A,%B %d,%Y')
    print(s,type(s))
    txt =('%s年%s月%s日星期%s %s时%s分%s秒'%(dt.strftime('%Y'),dt.strftime('%m'),dt.strftime('%d'),<br>dt.strftime('%w'),dt.strftime('%H'),dt.strftime('%M'),dt.strftime('%S')))
    print(txt)
    s=dt.strftime('%B %d,%Y')
    print(s,type(s))
     
    结果:
    07/07/2019 14:57:17 PM <class 'str'>
    Sunday,July 07,2019 <class 'str'>
    2019年07月07日星期0 14时57分17秒
    July 07,2019 <class 'str'>

    2. time

    import time
    
    # (1)当前时间戳
    # time.time()
    one = time.time()
    print(one)  # 1579454251.8242934
    
    # (2)时间戳 → 元组时间
    two = time.localtime(time.time())
    print(two)  # time.struct_time(tm_year=2020, tm_mon=1, tm_mday=20, tm_hour=1, tm_min=17, tm_sec=31, tm_wday=0, tm_yday=20, tm_isdst=0)
    
    # (3)时间戳 → 可视化时间
    three = time.ctime(time.time())
    print(three)  # Mon Jan 20 01:17:31 2020
    
    # (4)元组时间 → 时间戳
    four = time.mktime(two)
    print(four)  # 1579454251.0
    
    # (5)元组时间 → 可视化时间
    five = time.asctime()
    print(five)  # Mon Jan 20 01:17:31 2020
    
    
    # (6)时间元组 → 可视化时间(定制)
    six = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    print(six)  # 2020-01-20 01:17:31
    
    
    # (7)可视化时间(定制) → 时间元祖
    print("时间元祖:", time.strptime('2019-7-7 11:32:23', '%Y-%m-%d %H:%M:%S'))
    # 时间元祖: time.struct_time(tm_year=2019, tm_mon=7, tm_mday=7, tm_hour=11, tm_min=32, tm_sec=23, tm_wday=6, tm_yday=188, tm_isdst=-1)
    
    # (8)将格式字符串转换为时间戳
    eight = "Sun Jul  7 10:48:24 2019"
    print(time.mktime(time.strptime(eight, "%a %b %d %H:%M:%S %Y")))
    # 1562467704.0
    

      

  • 相关阅读:
    设计模式学习笔记之设计原则
    设计模式学习笔记之生成器模式
    设计模式学习笔记之适配器模式、外观模式
    java中的日期类型之间转换
    JS刷新当前页面的几种方法总结
    Java_枚举
    动态规划详解_2
    Java算法-动态规划详解
    Java经典算法题_2
    Java算法
  • 原文地址:https://www.cnblogs.com/yzg-14/p/12216442.html
Copyright © 2020-2023  润新知