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")
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.
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.
Use utcnow
:
today = datetime.datetime.utcnow().date()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With