Python datetime.utcnow() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
deprecated in 3.12

Definition

datetime.utcnow() returns the current date and time in UTC, but as a naive datetime: the numbers are UTC, yet tzinfo is None, so nothing in the object says so.

Because many methods treat naive values as local time, this causes silent bugs. Python 3.12 deprecated it: each call emits a DeprecationWarning. The replacement is datetime.now(timezone.utc), which returns an aware datetime (see now()).

datetime.utcnow() is a classmethod of datetime.datetime: call it on the class itself, as datetime.utcnow(...), and it returns a new object. Import the class first, for example from datetime import datetime. It still works in current versions, but new code should not use it.

Syntax

The old call and its modern replacement:

python
legacy = datetime.utcnow()                # deprecated, naive
moment = datetime.now(timezone.utc)       # recommended, aware
CallWhat it does
datetime.utcnow()Gives the current UTC time as a naive datetime; on Python 3.12+ it also emits DeprecationWarning.
datetime.now(timezone.utc)The recommended replacement: the current time as an aware UTC datetime.
datetime.now(timezone.utc).replace(tzinfo=None)Produces the same naive value utcnow() gave, for legacy code that still needs it.

The warning is printed to stderr, not stdout, and is hidden by default outside __main__ code and test runners; it does not change the returned value.

Method signatures
python
classmethod datetime.utcnow()
# deprecated since Python 3.12
# returns: naive datetime holding the current UTC time

Parameters

utcnow() takes no parameters.

Return Value

Return ValueDescription
datetimeA naive datetime whose fields hold the current UTC date and time; tzinfo is None.

Python Examples

Example 1 — Inspect the Naive Value utcnow() Returns

Call the deprecated method and look at the type and the zone. On Python 3.12+ a DeprecationWarning also appears on stderr.

python
from datetime import datetime

stamp = datetime.utcnow()

print(type(stamp).__name__)
print(stamp.tzinfo)
print(stamp.utcoffset())

How It Works

  1. Still a datetime. The call works and returns an ordinary datetime holding UTC numbers.
  2. No zone attached. tzinfo is None, so the object is naive and does not record that it means UTC.
  3. No offset either. utcoffset() returns None for every naive datetime.
  4. Warning on stderr. Python 3.12+ writes a DeprecationWarning to stderr; it is not part of the printed output above.

Example 2 — Why a Naive UTC Value Causes Errors

Use a fixed naive value shaped exactly like a utcnow() result and compare it with the same instant as an aware datetime.

python
from datetime import datetime, timezone

naive_utc = datetime(2024, 3, 15, 10, 30)
aware_utc = datetime(2024, 3, 15, 10, 30, tzinfo=timezone.utc)

try:
    print(naive_utc < aware_utc)
except TypeError:
    print("cannot compare naive and aware datetimes")
print(naive_utc == aware_utc)
print(naive_utc.replace(tzinfo=timezone.utc) == aware_utc)

How It Works

  1. Same numbers, different kinds. Both objects say 10:30 on 15 March 2024, but only aware_utc knows it is UTC.
  2. Ordering fails. < between naive and aware raises TypeError, which the except block reports.
  3. Equality is just False. == does not raise; it quietly says the values differ, which can hide bugs.
  4. Attach UTC to fix. replace() with tzinfo=timezone.utc labels the naive value correctly, and now the two are equal.

Example 3 — Replace utcnow() with an Aware Helper

Wrap the modern call in a helper, and convert aware values to naive UTC only at the edge where legacy code needs them.

python
from datetime import datetime, timedelta, timezone


def utc_now():
    return datetime.now(timezone.utc)


def as_naive_utc(moment):
    return moment.astimezone(timezone.utc).replace(tzinfo=None)


stamp = utc_now()
legacy = as_naive_utc(stamp)
print(stamp.tzinfo, legacy.tzinfo)
print(legacy == stamp.replace(tzinfo=None))
ist = timezone(timedelta(hours=5, minutes=30))
print(as_naive_utc(datetime(2024, 3, 15, 16, 0, tzinfo=ist)))

How It Works

  1. One place to change. utc_now() returns an aware UTC datetime and replaces every datetime.utcnow() call.
  2. Convert, then drop the zone. as_naive_utc() first converts to UTC with astimezone(), then removes tzinfo for old code that expects naive values.
  3. Values match. For a UTC input the conversion changes nothing, so the naive copy equals the aware value with its zone stripped.
  4. Fixed check. 16:00 at UTC+05:30 is 10:30 UTC, which the helper returns as a naive 2024-03-15 10:30:00.

Frequently Asked Questions

Yes. It is deprecated since Python 3.12 and emits a DeprecationWarning. Use datetime.now(timezone.utc) instead.
It returns a naive datetime. Methods such as timestamp() and astimezone() assume naive values are local time, so the result is wrong unless the machine runs in UTC.
datetime.now(timezone.utc). If old code needs a naive value, add .replace(tzinfo=None) at the point where you hand it over.
Python hides DeprecationWarning by default unless the call is in __main__ or you run with -W default or a test runner. The warning goes to stderr.
The clock values are the same UTC reading; only the tzinfo differs. utcnow() leaves it as None.

Next: date.weekday()

Return the day of the week as 0 (Monday) to 6 (Sunday).

weekday() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful