千家信息网

Python中的datetime包与time包怎么用

发表于:2024-11-28 作者:千家信息网编辑
千家信息网最后更新 2024年11月28日,这篇文章主要介绍"Python中的datetime包与time包怎么用",在日常操作中,相信很多人在Python中的datetime包与time包怎么用问题上存在疑惑,小编查阅了各式资料,整理出简单好
千家信息网最后更新 2024年11月28日Python中的datetime包与time包怎么用

这篇文章主要介绍"Python中的datetime包与time包怎么用",在日常操作中,相信很多人在Python中的datetime包与time包怎么用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Python中的datetime包与time包怎么用"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

    一、datetime包

    1.timedelta(params…)得到一个时间增量对象

    # coding:utf-8from datetime import timedeltaif __name__ == '__main__':    # 常用参数 hours:小时 days:天 seconds:秒 milliseconds:毫秒    delta = timedelta(hours=2)    print(delta)  # 2:00:00    print(type(delta))  # 

    2.timezone(timedelta) + timedelta(params…) 创建时区对象

    # coding:utf-8from datetime import timedelta, timezoneif __name__ == '__main__':    delta = timedelta(hours=2)    zone = timezone(delta)  #配合timedelta创建时区对象    print(zone)  # UTC+02:00    print(type(zone))  # 

    3.datetime模块

    datetime.now(timezone) 获取当前时间datetime对象# coding:utf-8from datetime import timedelta, timezone, datetimeif __name__ == '__main__':    '''    获取当前时间,可以获取指定时区的当前时间    datetime.now(timezone)    '''    now = datetime.now()    print(now)  # 2022-02-23 13:59:59.224286    print(type(now))  #     # 设置指定时区的当前时间    print(datetime.now((timezone(timedelta(hours=9)))))  # 2022-02-23 14:59:59.224286+09:00
    datetime.strftime(fmt) datetime时间对象转字符串
    # coding:utf-8from datetime import datetimeif __name__ == '__main__':    '''    datetime.strftime(fmt)    将时间对象转换成字符串    fmt:格式化标准,由格式符组成    常用格式符(年:%Y,月:%m,日:%D,时:%H,分:%M,秒:%S)    '''    now = datetime.now()    print(now.strftime('%Y-%m-%d %H:%M:%S'))  # 2022-02-23 14:04:24
    datetime.strptime(date_string,fmt) 字符串转成datetime时间对象
    # coding:utf-8from datetime import datetimeif __name__ == '__main__':    '''    datetime.strptime(date_string,fmt)    将字符串转换成时间对象,要求date_string的格式完全匹配fmt格式化标准    '''    time_obj = datetime.strptime('2022-2-22', '%Y-%m-%d')    # datetime.strptime('2022-2-22', '%Y-%m-%d %H') Error date_string 中不存在小时而fmt中要求有小时    print(datetime.strptime('2022-2-22 14', '%Y-%m-%d %H'))  # 2022-02-22 14:00:00    print(time_obj)  # 2022-02-22 00:00:00    print(type(time_obj))  # 
    datetime.timestamp(datetime_obj) 将datetime时间对象转换成秒级时间戳
    # coding:utf-8from datetime import datetimeif __name__ == '__main__':    '''    datetime.timestamp(datetime_obj)     datetime_obj:datetime 时间对象    返回 float    '''    print(datetime.timestamp(datetime.now()))  # 1645598565.715
    datetime.fromtimestamp(t) 将秒级时间戳转换成datetime时间对象
    # coding:utf-8from datetime import datetime, timedelta, timezoneif __name__ == '__main__':    '''    datetime.fromtimestamp(t)    t:秒级时间戳 float类型    返回:datetime时间对象    '''    datetime_obj = datetime.fromtimestamp(1645598565.715)    print(datetime_obj)  # 2022-02-23 14:42:45.715000    print(type(datetime_obj))  # 

    4.使用datetime对象 + timedelta(params…) 进行时间运算

    # coding:utf-8from datetime import datetime, timedelta, timezoneif __name__ == '__main__':    now = datetime.now()    fmt = '%Y-%m-%d %H:%M:%S'    print(now.strftime(fmt))  # 2022-02-23 15:07:01    # 3小时后时间    print((now + timedelta(hours=3)).strftime(fmt))  # 2022-02-23 18:07:01    # 3小时前时间    print((now - timedelta(hours=3)).strftime(fmt))  # 2022-02-23 12:07:01    print((now + timedelta(hours=-3)).strftime(fmt))  # 2022-02-23 12:07:01    # 建议timedelta的参数都使用正数(容易理解)

    二、time包

    1.time.time() 得到当前秒级时间戳

    # coding:utf-8import timeif __name__ == '__main__':    print(time.time())  # 1645667203.7236724

    2.time.localtime(second) 将秒转换成time时间对象

    # coding:utf-8import timeif __name__ == '__main__':    # second 不填,则默认当前的时间戳    t = time.localtime(time.time())    t2 = time.localtime()    print(t)  # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0)    print(t2)  # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0)    print(type(t))  #     print(type(t2))  # 

    3.time.strftime(fmt,time_obj) 将time时间对象转换成字符串

    # coding:utf-8import timeif __name__ == '__main__':    """    time.strftime(fmt,time_obj)    fmt:格式化标准 参考 datetime.strftime(fmt)    time_obj:time时间对象,不填默认是当前日期的time时间对象    """    t = time.localtime(time.time() + 3600)    print(time.strftime('%Y-%m-%d %H:%M:%S'))  # 2022-02-24 10:16:17    print(time.strftime('%Y-%m-%d %H:%M:%S', t))  # 2022-02-24 11:16:17

    4.time.strptime(time_string,fmt) 将字符串转换成time时间对象

    # coding:utf-8import timeif __name__ == '__main__':    """    time.strptime(time_string,fmt)    参考 datetime.strptime(date_string,fmt)    time_string:时间字符串    fmt:格式化标准    """    fmt = '%Y-%m-%d %H:%M:%S'    t = time.strftime(fmt, time.localtime())    print(t)  # 2022-02-24 10:25:17    print(time.strptime(t, fmt))  # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=25, tm_sec=40, tm_wday=3, tm_yday=55, tm_isdst=-1)

    5.time.sleep(second) 休眠 second 秒

    # coding:utf-8import timeif __name__ == '__main__':    print(time.time())  # 1645670183.6567423    time.sleep(2)    print(time.time())  # 1645670185.6708047

    到此,关于"Python中的datetime包与time包怎么用"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

    0