如何确保
datetime.date.today()
转换为UTC时间?
这是我到目前为止的代码:
#today : 2014-12-21
today = datetime.date.today()
#1900-01-01 16:00:00+00:00
timeformat = datetime.datetime.strptime('16:00', '%H:%M').replace(tzinfo=pytz.utc)
#combine today and timeformat 2014-12-21 16:00:00
now = datetime.datetime.combine(u, timeformat.time())
str_now = now.strftime("%Y-%m-%d %H:%M:%S")
使用
utcnow
:
today = datetime.datetime.utcnow().date()
由于 utcnow 现已弃用,python3 解决方案是:
from datetime import datetime, timezone
utc_todays_date = datetime.now(timezone.utc).date()
要打印带有时间的日期,您可以使用...
tomorrow = twtomorrow.strftime("%Y-%m-%d %H:%M:%S")
而不是
tomorrow = twtomorrow.strftime("%Y-%m-%d")