• Python对于时间相关模块的学习记录(time,datetime等模块)


    1,time.time() 获得从计算机开始出生到现在的秒数(也成时间戳),可以时间相减计算流逝时间

    说明 :擅长时间相减计算流逝时间

    导入方法 import time

    import time
    
    # 1,time.time     获得从计算机开始出生到现在的秒数,可以时间相减计算流逝时间
    ret = time.time()
    print(ret)    # 1698479623.4234037
    
    start_time = time.time()
    # 休眠五秒
    time.sleep(5)
    end_time = time.time()
    print(end_time - start_time)     # 5.006281137466431
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    2,strftime 获得从计算机开始出生到现在的秒数(也成时间戳),可以时间相减计算流逝时间

    说明 :擅长以特定格式获得数间数据

    导入方法 from time import strftime

    from time import strftime
    
    # 擅长以特定格式获得数间数据
    
    #               年     月  日    时    分   秒     星期
    print(strftime(r'%Y   %m   %d   %H   %M   %S     %A'))
    # =====》 2023   10   28   16   05   39     Saturday
    
    
    # %x 月/日/年    %X时分秒
    print(strftime(r'%x     %X'))
    # =====》  10/28/23     16:07:40
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    3,localtime 擅长结构化时间

    说明 :擅长结构化时间

    导入方法 from time import localtime

    from time import localtime
    
    ret = localtime()
    print(ret)
    # ====> time.struct_time(tm_year=2023, tm_mon=10, tm_mday=28, tm_hour=16, tm_min=11, tm_sec=29, tm_wday=5, tm_yday=301, tm_isdst=0)
    
    # 年
    print(ret.tm_year)   # 2023
    # 月
    print(ret.tm_mon)    # 10
    # 日
    print(ret.tm_mday)     # 28
    #时
    print(ret.tm_hour)     # 16
    #分
    print(ret.tm_min)      # 14
    # 秒
    print(ret.tm_sec)      # 9
    # 一周的第几天,从0开始  礼拜六显示5
    print(ret.tm_wday)     # 5
    # 一年的第几天
    print(ret.tm_yday)     # 301
    # 是否是夏令时
    print(ret.tm_isdst)     # 0
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    4,datetime 擅长进行时间的相加减

    说明 :擅长进行时间的相加减

    导入方法 import datetime

    import datetime
    
    ret = datetime.datetime.now()
    print(ret)   # 2023-10-28 16:31:56.007193
    
    # 微妙为0 (注意不是微秒的精度),而是强行给定一个微秒的数值
    print(ret.replace(microsecond=0))   # 2023-10-28 16:33:57
    print(ret.replace(microsecond=4))   # 2023-10-28 16:35:03.000004
    
    
    # 当前时间加上一段时间的时间
    
    target_time = datetime.datetime.now() + datetime.timedelta(5)
    # datetime.timedelta()参数
    # days: float = ...,
    # seconds: float = ...,
    # microseconds: float = ...,
    # milliseconds: float = ...,
    # minutes: float = ...,
    # hours: float = ...,
    # weeks: float = ..., *, fold: int = ...)
    
    print(target_time)   # 2023-11-02 16:37:37.725015
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    详细情况参考哔站有个大佬的视频

    大佬视频传送门

  • 相关阅读:
    Redis简介
    CF1765M Minimum LCM
    SpringBoot整合MyBatis和Redis
    2023/10/25
    坑爹,线上同步近 3w 个用户导致链路阻塞引入发的线上问题,你经历过吗?
    07_ElasticSearch:倒排序索引与分词Analysis
    将 ChatGPT 与 ReactJS 集成以实现更智能的对话界面
    1027. 最长等差数列(leetcode)
    2022-08-26 Unity视频播放4——全景视频
    基于JAVA网上花店计算机毕业设计源码+数据库+lw文档+系统+部署
  • 原文地址:https://blog.csdn.net/weixin_45668674/article/details/134092337