如何将datetime.date.today()转换为UTC时间?

时间:2014-12-21 06:03:06

标签: python pytz

如何确保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")

2 个答案:

答案 0 :(得分:33)

使用utcnow

today = datetime.datetime.utcnow().date()

答案 1 :(得分:-5)

用于打印日期和时间可以使用...

tomorrow =  twtomorrow.strftime("%Y-%m-%d %H:%M:%S")

而不是

tomorrow =  twtomorrow.strftime("%Y-%m-%d")