• 【Python】Python常用的日期时间函数


    Python常用的日期时间函数

    在Python中,日期和时间可能涉及好几种不同的数据类型和 函数。下面回顾了表示时间的3种不同类型的值:

    Unix纪元时间戳( time 模块中使用)是一个浮点值或 整型值,表示自1970年1月1日午夜0点 (UTC) 以来的秒数。

    datetime对象(属于datetime模块)包含一些整 型值,保存在year、month、day、 hour、minute和second等属性中。

    timedelta 对象(属于 datetime 模块)表示的一段 时间,而不是一个特定的时刻。

    下面回顾了时间函数及其参数和返回值:

    time.time() 函数返回一个浮点值, 表示当前时刻的Unix 纪元时间戳。

    time.sleep(seconds)函数让程序 暂停seconds参数指定的秒数。

    datetime.datetime(year, month, day,hour,minute, second) 函数返回参数指定的时刻的datetime对象。 如果没有提供hour、minute或 second参数,它们默认为0。

    datetime.datetime.now()函数返回当前 时刻的datetime对象。

    datetime.datetime.fromtimestamp(epoch)函数 返回 epoch时间戳参数表示的时刻的datetime对象。

    datetime.timedelta(weeks, days, hours,minutes, seconds, milliseconds, microseconds) 函数返回一个表示一段时间的 timedelta 对象。 该函数的关键字参数都是可选的,不包括 month 或 year 。

    total_seconds() 方法用于timedelta对象, 返回timedelta对象表示的秒数。

    strftime(format)方法返回一个字符串, 用 format 字符串中的定制格式来表示 datetime 对象表不的时间。详细格式参见表15-1。

    datetime.datetime.strptime(time_string,format)函数 返回一个datetime对象,它的时刻由time_string指定, 利用format字符串参数来解析。

  • 相关阅读:
    基于springboot实现疫情管理系统
    KubeVela 再升级:交付管理一体化的云原生应用平台
    学会Linux,看完这篇就行了!
    Linux kill -9 {进程名称}
    Android 发布自己的sdk
    「点燃我,温暖你」用Python制作一个动态爱心效果
    【博客478】prometheus-----存储目录结构以及格式,作用分析
    【Java】线程池、Lambda表达式
    vue form表单验证
    【C++题解】1632. 需要几辆车
  • 原文地址:https://blog.csdn.net/wjianwei666/article/details/133810654