Python3.4 datetime.today() 和 datetime.now() [英] Python3.4 datetime.today() and datetime.now()

查看:62
本文介绍了Python3.4 datetime.today() 和 datetime.now()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.today()
datetime.datetime(2015, 8, 30, 23, 17, 6, 937659)
>>> datetime.datetime.now()
datetime.datetime(2015, 8, 30, 23, 17, 14, 378097)
>>>

在 Python3.4 中 datetime.today() 和 datetime.now() 有什么区别?

In Python3.4 datetime.today() and datetime.now() What's the difference?

推荐答案

根据datetime.now(tz=None)的文档:

返回当前本地日期和时间.如果可选参数 tzNone 或未指定,则类似于 today(),但是,如果可能,提供比可以获得的精度更高的精度通过 time.time() 时间戳(例如,这在提供 C gettimeofday() 函数的平台上是可能的).

Return the current local date and time. If optional argument tz is None or not specified, this is like today(), but, if possible, supplies more precision than can be gotten from going through a time.time() timestamp (for example, this may be possible on platforms supplying the C gettimeofday() function).

Else tz 必须是 tzinfo 类子类的实例,当前日期和时间转换为 tz 的时区.在这种情况下,结果等效于 tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).

Else tz must be an instance of a class tzinfo subclass, and the current date and time are converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)).

因此 datetime.now() 尽可能提供更高的精度.

Thus datetime.now() supplies more precision if possible.

这篇关于Python3.4 datetime.today() 和 datetime.now()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆