# Python学习之datetime时间戳

>>> import datetime
>>> datetime.datetime.utcnow()
datetime.datetime(2018, 4, 17, 8, 4, 14, 924480)
>>> datetime.datetime.now()
datetime.datetime(2018, 4, 17, 16, 4, 46, 821245)


python手册中是这样写的：

classmethod datetime.now(tz=None)
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).
If tz is not None, it must be an instance of a 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)). See also today(), utcnow().

classmethod datetime.utcnow()
Return the current UTC date and time, with tzinfo None. This is like now(), but returns the current UTC date and time, as a naive datetime object. An aware current UTC datetime can be obtained by calling datetime.now(timezone.utc). See also now().

>>> import datetime
>>> import iso8601
>>> import pytz
>>> def utcnow():
...    return datetime.datetime.now(tz=pytz.utc)
...
>>> utcnow()
datetime.datetime(2018, 4, 17, 8, 7, 41, 300399, tzinfo=<UTC>)
>>> iso8601.parse_date(utcnow().isoformat())
datetime.datetime(2018, 4, 17, 8, 10, 32, 70631, tzinfo=datetime.timezone(datetime.timedelta(0), '+00:00'))


• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120