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 OverflowPython 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.
Top answer 1 of 10
90
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)
2 of 10
11
First you need to make sure the datetime is a timezone-aware object by setting its tzinfo member:
http://docs.python.org/library/datetime.html#datetime.tzinfo
You can then use the .astimezone() function to convert it:
http://docs.python.org/library/datetime.html#datetime.datetime.astimezone
Videos
01:21
How to Convert a datetime Object to UTC in Python - YouTube
01:00
How to Convert UTC Time Zones in Python! #python #programming #coding ...
Convert UTC to local timezone - Python
02:01
How to Convert a datetime to UTC Timestamp in Python3 - YouTube
03:18
python datetime now utc - YouTube
02:12
Convert UTC to local timezone - Python - YouTube
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 ·
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).
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)
Plainenglish
aws.plainenglish.io › 11-costly-aws-habits-that-look-smart-until-you-see-the-bill-41898dfe8ab4
11 Costly AWS Habits That Look Smart… Until You See the ...
February 24, 2026 - New AWS, Cloud, and DevOps content every day. Follow to join our 3.5M+ monthly readers.
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 ·
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.