【问题标题】:What is the difference between python functions `datetime.now()` and `datetime.today()`?python函数`datetime.now()`和`datetime.today()`有什么区别?
【发布时间】:2015-09-11 06:49:19
【问题描述】:

python函数datetime.now()datetime.today()有什么区别?

在[1]中:从日期时间导入日期时间

在[2]中:datetime.now()

Out[2]: datetime.datetime(2015, 9, 11, 12, 8,28, 909842)

在[3]中:datetime.today()

Out[3]: datetime.datetime(2015, 9, 11, 12,8, 45, 175839)

提前致谢。

【问题讨论】:

    标签: python datetime


    【解决方案1】:

    datetime.datetime.now() 采用 tzinfo 作为关键字参数,但 datetime.today() 不采用任何关键字参数。

    默认情况下,now() 使用 datetime.datetime.now(tz=None)

    执行

    在文档中引用:https://docs.python.org/3.6/library/datetime.html#datetime.datetime.now

    datetime.now() 返回当前的本地日期和时间。如果可选 参数 tz 为 None 或未指定,这就像 today(),但是,如果 可能,提供的精度比从运行中获得的精度更高 通过 time.time() 时间戳(例如,这可能在 提供 C gettimeofday() 函数的平台)。

    【讨论】:

    • 顺便说一句,要指定 ET 时区,我使用import datetime.datetime, pytz; datetime.datetime.now(tz=pytz.timezone('US/Eastern'))。更多详情here.
    【解决方案2】:

    请参阅datetime.todaydatetime.now 的文档,特别是后面链接中的这一部分:

    如果可选参数 tz 为 None 或未指定,这类似于 today(),但如果可能,提供比通过 time.time() 时间戳获得的精度更高的精度(例如,这可能是可能的在提供 C gettimeofday() 函数的平台上)。

    因此,在您的示例中,两者是相同的,尽管特定平台可能会通过 datetime.now 提供更高的精度。

    【讨论】:

      【解决方案3】:

      参见the documentationnow() 提供了一个可选的时区,并且可以提供更高的精度。

      【讨论】:

        猜你喜欢
        • 2018-07-18
        • 1970-01-01
        • 2018-02-11
        • 2013-01-12
        • 1970-01-01
        • 2016-03-26
        • 1970-01-01
        • 2019-01-25
        • 1970-01-01
        相关资源
        最近更新 更多