DEV Community

Cover image for Python: it is now() time to migrate from utcnow()
Andrea Grandi
Andrea Grandi

Posted on

Python: it is now() time to migrate from utcnow()

This morning I randomly found this post from Miguel Grinberg: It's Time For A Change: datetime.utcnow() Is Now Deprecated.

The main point is that Python utcnow() method is not timezone aware, and Python 3.12 is deprecating it. Therefore, you should start migrating your code to use now() instead.

Current state until Python 3.11

Until Python 3.11, the utcnow() method returns a datetime object, and you would use it like this:

>>> from datetime import datetime
>>> datetime.utcnow()
datetime.datetime(2024, 7, 17, 12, 17, 9, 835551)
Enter fullscreen mode Exit fullscreen mode

The issue with this method is that it doesn't include timezone information, so you can't be certain if the time is in UTC or not.

(the post continues on my blog https://www.andreagrandi.it/posts/python-now-time-to-migrate-from-utcnow/ )

Top comments (0)