• Python标准库timet和datetime模块用法


    Python的time和datetime模块提供了各种与日期时间相关的函数。

    一、time模块

    time() 当前时间(新纪元1970年1月1日到访问时的秒数,也叫时间戳)
    localtime([secs]) 将时间戳转换为表示当地时间的间元组
    mktime(tuple) 将时间元组转换为时间戳
    sleep(sesc) 休眠secs秒
    strptime(string[, format]) 将字符串转换为时间元组
    strftime(format[, t]) 将时间元组转换为字符串

    import time 
    
    #当前时间的不同格式:时间戳、时间元组、字符串
    print(time.time())
    print(time.localtime())
    print(time.strftime("%Y-%m-%d %H:%M:%S"))
    #时间戳转时间元组
    tup1 = time.localtime(1611331200)
    print(tup1)
    #时间元组转时间戳
    print(time.mktime(tup1))
    #时间元组转字符串
    str1 = time.strftime("%Y-%m-%d %H:%M:%S", tup1)
    print(str1)
    #字符串转时间元组
    tup2 = time.strptime(str1, "%Y-%m-%d %H:%M:%S")
    print(tup2)
    
    '''结果
    1611386684.0896702
    time.struct_time(tm_year=2021, tm_mon=1, tm_mday=23, tm_hour=15, tm_min=24, tm_sec=44, tm_wday=5, tm_yday=23, tm_isdst=0)
    2021-01-23 15:24:44
    time.struct_time(tm_year=2021, tm_mon=1, tm_mday=23, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=23, tm_isdst=0)
    1611331200.0
    2021-01-23 00:00:00
    time.struct_time(tm_year=2021, tm_mon=1, tm_mday=23, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=23, tm_isdst=-1)
    '''
    

    二、datetime模块

    datetime 模块提供用于处理日期和时间的类。
    在支持日期时间数学运算的同时,实现的关注点更着重于如何能够更有效地解析其属性用于格式化输出和数据操作。

    datetime模块定义了以下几个类:
    datetime.date 表示日期
    datetime.time 表示时间
    datetime.datetime 表示日期时间
    datetime.timedelta 表示两个date、time、datetime实例之间的时间间隔
    datetime.tzinfo 时区相关信息对象的抽象基类
    datetime.timezone

    datetime.datetime部分类方法
    datetime.today() 返回一个表示当前日期时间的datetime对象
    datetime.now([tz]) 返回指定时区日期时间的datetime对象,如果不指定tz参数则结果同上
    datetime.fromtimestamp(timestamp[, tz]) 根据指定的时间戳创建一个datetime对象
    datetime.strptime(date_str, format) 将时间字符串转换为datetime对象
    datetime.datetime部分对象方法
    dt.year, dt.month, dt.day 年、月、日
    dt.hour, dt.minute, dt.second 时、分、秒
    dt.strftime(format) 返回指定格式的时间字符串

    import time,datetime,random
    
    #返回当前日期时间的datetime对象
    now = datetime.datetime.today()
    print(now)
    print(datetime.datetime.now())
    
    #年、月、日、时、分、秒
    print(now.year, now.month, now.day, now.hour, now.minute, now.second)
    
    #时间戳转datetime对象
    dt = datetime.datetime.fromtimestamp(1611331200)
    print(dt)
    
    #返回datetime对象的指定格式时间字符串
    date_str = dt.strftime("%Y-%m-%d %H:%M:%S")
    print(date_str)
    
    #时间字符串转换为datetime对象
    dt = datetime.datetime.strptime(date_str,"%Y-%m-%d %H:%M:%S")
    print(dt)
    
    '''结果:
    2021-01-23 16:43:26.496192
    2021-01-23 16:43:26.496191
    2021 1 23 16 43 26
    2021-01-23 00:00:00
    2021-01-23 00:00:00
    2021-01-23 00:00:00
    '''
    

      

  • 相关阅读:
    Processing中如何记录Sketch运行时间
    交互设计算法基础(11)- Merge Sort
    交互设计算法基础(10)- Quick Sort
    交互设计算法基础(9)- Bubble Sort
    交互设计算法基础(8)- Heap Sort
    交互设计算法基础(7)- Straight Selection Sort
    交互设计算法基础(6)- Shells Sort
    交互设计算法基础(5)- Straight Insertion Sort
    ZOOM 似乎无法连接。请检查您的网络连接,然后重试。【已解决】
    Android下通过root实现对system_server中binder的ioctl调用拦截
  • 原文地址:https://www.cnblogs.com/gdjlc/p/14317746.html
Copyright © 2020-2023  润新知