Utc Datetime Python

Posted on by admin

Question or problem about Python programming:

Converting datetime.date to UTC timestamp in Python. Ask Question Asked 9 years, 3 months ago. Active 1 year, 1 month ago. Viewed 644k times 323. I am dealing with dates in Python and I need to convert them to UTC timestamps to be used inside Javascript. The following code does not work. Fortunately, Python datetime provides a method called.strptime to handle this situation. This method uses a special mini-language to tell Python which parts of. Converting the date object first to datetime also does not help. I tried the example at this link from, but: from pytz import utc, timezone from datetime import datetime from time import mktime inputdate = datetime (year = 2011, month = 1, day = 15) and now either: mktime (utc. Localize (inputdate). Utctimetuple ).


I have a python datetime instance that was created using datetime.utcnow() and persisted in database.

For display, I would like to convert the datetime instance retrieved from the database to local datetime using the default local timezone (i.e., as if the datetime was created using datetime.now()).

How can I convert the UTC datetime to a local datetime using only python standard library (e.g., no pytz dependency)?

It seems one solution would be to use datetime.astimezone( tz ), but how would you get the default local timezone?

How to solve the problem:

Solution 1:

In Python 3.3+:

In Python 2/3:

Using pytz (both Python 2/3):


Python 3.3

Utc datetime python programming

Python 2


Note: it takes into account DST and the recent change of utc offset for MSK timezone.

I don’t know whether non-pytz solutions work on Windows.

Utc Datetime Python Tutorial

Solution 2:


You can’t do it with only the standard library as the standard library doesn’t have any timezones. You need pytz or dateutil.

Or well, you can do it without pytz or dateutil by implementing your own timezones. But that would be silly.

Solution 3:

You can’t do it with standard library. Using pytz module you can convert any naive/aware datetime object to any other time zone. Lets see some examples using Python 3.

Naive objects created through class method utcnow()

To convert a naive object to any other time zone, first you have to convert it into aware datetime object. You can use the replace method for converting a naive datetime object to an aware datetime object. Then to convert an aware datetime object to any other timezone you can use astimezone method.

The variable pytz.all_timezones gives you the list of all available time zones in pytz module.

Naive objects created through class method now()


Because now method returns current date and time, so you have to make the datetime object timezone aware first. The localize function converts a naive datetime object into a timezone-aware datetime object. Then you can use the astimezone method to convert it into another timezone.

Solution 4:

Building on Alexei’s comment. This should work for DST too.

Python Datetime Timezone

Solution 5:

I think I figured it out: computes number of seconds since epoch, then converts to a local timzeone using time.localtime, and then converts the time struct back into a datetime…

It applies the summer/winter DST correctly:

Python 3.6 Datetime Convert To Utc

Hope this helps!