Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert datetime.date.today() to UTC time?

Tags:

python

pytz

How to make sure the datetime.date.today() is converted to UTC time?

This is my code so far:

#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") 
like image 249
user2492364 Avatar asked Dec 21 '14 06:12

user2492364


People also ask

How do you convert datetime to UTC?

The ToUniversalTime method converts a DateTime value from local time to UTC. To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method.

How do you convert datetime to UTC datetime in Python?

You can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object.


1 Answers

Use utcnow:

today = datetime.datetime.utcnow().date() 
like image 109
Daniel Avatar answered Sep 19 '22 21:09

Daniel