Run this to obtain a naive datetime in UTC (and to add five minutes to it):

>>> from datetime import datetime, timedelta
>>> datetime.utcnow()
datetime.datetime(2021, 1, 26, 15, 41, 52, 441598)
>>> datetime.utcnow() + timedelta(minutes=5)
datetime.datetime(2021, 1, 26, 15, 46, 52, 441598)

If you would prefer a timezone-aware datetime object, run this in Python 3.2 or higher:

>>> from datetime import datetime, timezone
>>> datetime.now(timezone.utc)
datetime.datetime(2021, 1, 26, 15, 43, 54, 379421, tzinfo=datetime.timezone.utc)
Answer from Senyai on Stack Overflow
🌐
Python documentation
docs.python.org › 3 › library › datetime.html
datetime — Basic date and time types
The datetime module has a basic timezone class (for handling arbitrary fixed offsets from UTC) and its timezone.utc attribute (a UTC timezone instance). zoneinfo brings the IANA time zone database (also known as the Olson database) to Python, and its usage is recommended.
🌐
GeeksforGeeks
geeksforgeeks.org › python › get-utc-timestamp-in-python
Get UTC timestamp in Python - GeeksforGeeks
July 12, 2025 - Use the datetime.datetime.now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.
🌐
Python Morsels
pythonmorsels.com › converting-to-utc-time
Converting datetime to UTC in Python - Python Morsels
December 20, 2021 - >>> utc_april_fools = april_fools.astimezone(datetime.timezone.utc) >>> utc_april_fools datetime.datetime(2030, 4, 1, 17, 0, tzinfo=datetime.timezone.utc) >>> print(utc_april_fools) 2030-04-01 17:00:00+00:00 · Or on Python 3.11+ you can use datetime.UTC instead of datetime.timezone.utc: >>> utc_april_fools = april_fools.astimezone(datetime.UTC) >>> utc_april_fools datetime.datetime(2030, 4, 1, 17, 0, tzinfo=datetime.UTC) >>> print(utc_april_fools) 2030-04-01 17:00:00+00:00 ·
🌐
Simon Willison
til.simonwillison.net › python › utc-warning-fix
Fixes for datetime UTC warnings in Python | Simon Willison’s TILs
December 13, 2024 - I found another hint in the docs for replacing the now deprecated datetime.datetime.utcfromtimestamp: Warning: Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC.
🌐
Python.org
discuss.python.org › python help
Why is datetime.utcnow deprecated? - Python Help - Discussions on Python.org
April 2, 2025 - Just made an account to say that I agree with everyone in Deprecating `utcnow` and `utcfromtimestamp` saying that this is a bad idea. I feel it’s a case of one person forcing their own coding preferences on everybody els…
🌐
CoinGecko
coingecko.com › learn › how-to-fetch-historical-crypto-data-with-python
How to Fetch Historical Crypto Data with Python | CoinGecko API
4 weeks ago - Running this script produces a DataFrame with a datetime index and columns for price, market cap, and volume. 💡Pro-Tip: The API automatically optimizes data granularity based on your date range. Shorter ranges return finer intervals, while longer ranges return daily data points (recorded at 00:00 UTC).
Find elsewhere
🌐
CodeSpeedy
codespeedy.com › home › how to get utc time in python
How to get UTC time in Python - CodeSpeedy
January 27, 2022 - # importing all required libraries import pytz import datetime #creating datetime object dt = datetime.datetime.now() #printing the current exact time print(dt) #Now we convert this time to UTC ut = datetime.datetime.now(pytz.utc) # utt = ut.timestamp() #now we print UTC time print(ut)
🌐
Medium
medium.com › @life-is-short-so-enjoy-it › python-datetime-utcnow-maybe-no-more-for-me-221795e8ddbf
python: datetime.utcnow() — maybe no more for me | by Life-is-short--so--enjoy-it | Medium
February 24, 2023 - The output that got my attention was the output of datetime.utcnow().timestamp() which is 1601444278 on UTC+7 timezone.
🌐
Real Python
realpython.com › python-datetime
Using Python datetime to Work With Dates and Times – Real Python
September 25, 2023 - Warning: datetime also provides datetime.utcnow(), which returns an instance of datetime at the current UTC. However, the Python documentation recommends against using this method because it doesn’t include any time zone information in the resulting instance.
🌐
Python
docs.python.org › 3 › library › index.html
The Python Standard Library — Python 3.14.3 documentation
While The Python Language Reference describes the exact syntax and semantics of the Python language, this library reference manual describes the standard library that is distributed with Python. It...
🌐
Django
docs.djangoproject.com › en › 6.0 › intro › tutorial02
Writing your first Django app, part 2 | Django documentation | Django
>>> q.save() # Now it has an ID. >>> q.id 1 # Access model field values via Python attributes. >>> q.question_text "What's new?" >>> q.pub_date datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=datetime.UTC) # Change values by changing the attributes, then calling save().
🌐
Paul Ganssle
blog.ganssle.io › articles › 2019 › 11 › utcnow.html
Paul Ganssle - Stop using utcnow and utcfromtimestamp
October 9, 2022 - The .timestamp() method gives a representation of a fixed point in time, not a point on the calendar; it returns Unix time, which is the number of seconds since 1970-01-01T00:00:00 UTC, and if you call it on a naïve datetime, Python will assume that that datetime represents your machine's ...
🌐
GitHub
github.com › python › cpython › issues › 91928
Add `datetime.UTC` alias for `datetime.timezone.utc` · Issue #91928 · python/cpython
April 25, 2022 - Since datetime.timezone is a class, not a module, right now it is necessary for each person to define their own UTC alias (if they want such a thing) in two lines:
Author   pganssle
🌐
Python
bugs.python.org › file13029 › datetime-utc-doc.patch
https://bugs.python.org/file13029/datetime-utc-doc...
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide
🌐
Astral
docs.astral.sh › ruff › rules › datetime-timezone-utc
datetime-timezone-utc (UP017) | Ruff
Checks for uses of datetime.timezone.utc. As of Python 3.11, datetime.UTC is an alias for datetime.timezone.utc. The alias is more readable and generally preferred over the full path. import datetime datetime.timezone.utc · Use instead: import datetime datetime.UTC ·
🌐
DEV Community
dev.to › behainguyen › python-local-date-time-and-utc-date-time-4cl7
Python: local date time and UTC date time. - DEV Community
February 11, 2023 - The time zone name for the local date time is None, and UTC for UTC date time. These are in conformance with datetime.tzname(). However, the first time I wrote this code, I was expecting either AUS Eastern Standard Time or AUS Eastern Summer Time for the local date time!
🌐
GitHub
gist.github.com › nicc777 › b0f5e4af9b01d8b05fd4ae4816d11d8b
Python Datetime Changes for UTC from 3.12 · GitHub
from datetime import datetime, timezone def aware_utcnow(): return datetime.now(timezone.utc) def aware_utcfromtimestamp(timestamp): return datetime.fromtimestamp(timestamp, timezone.utc) def naive_utcnow(): return aware_utcnow().replace(tzinfo=None) def naive_utcfromtimestamp(timestamp): return aware_utcfromtimestamp(timestamp).replace(tzinfo=None) print(aware_utcnow()) print(aware_utcfromtimestamp(0)) print(naive_utcnow()) print(naive_utcfromtimestamp(0)) ... if you are using Python 3.11 or newer, you can replace datetime.timezone.utc with a shorter datetime.UTC.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-datetime-utcoffset-method-with-example
Python datetime.utcoffset() Method with Example - GeeksforGeeks
September 6, 2024 - The utcoffset() function is used to return a timedelta object that represents the difference between the local time and UTC time. This function is used in used in the datetime class of module datetime.