Coding

Python datetime.now(): Get the Current Time, Local or UTC

In Python, datetime.now() returns the current local date and time: from datetime import datetime, then datetime.now(). For the current time in UTC, pass a time zone instead: datetime.now(timezone.utc).

from datetime import datetime, timezone

local_now = datetime.now()               # local time, naive
utc_now = datetime.now(timezone.utc)     # UTC, timezone-aware

print(local_now)
print(utc_now)

Example output (your values will differ):

2026-11-18 14:05:09.123456
2026-11-18 12:05:09.123456+00:00

The two lines look similar, but they are different kinds of object. The first has no time zone attached. The second knows it is UTC, and that is the +00:00 at the end. Most of the bugs people hit with the current time come from mixing the two.

What datetime.now() returns

The Python documentation describes datetime.now(tz=None) as returning "the current local date and time". With no argument, the result is like today(), but it supplies more precision where the platform allows it. If you pass a tz value, it must be an instance of a tzinfo subclass, and the current time is converted to that zone.

The result is a datetime object with attributes you can read directly:

now = datetime.now()
now.year, now.month, now.day        # (2026, 11, 18)
now.hour, now.minute, now.second    # (14, 5, 9)
now.microsecond                     # 123456
now.date()                          # datetime.date(2026, 11, 18)
now.time()                          # datetime.time(14, 5, 9, 123456)

Note the import. from datetime import datetime gives you the class, so you write datetime.now(). If you use import datetime instead, the name datetime is the module, and you need datetime.datetime.now(). Writing datetime.now() after import datetime fails with AttributeError: module 'datetime' has no attribute 'now'.

Naive vs aware: why the difference matters

The documentation splits date and time objects into "aware" and "naive". A naive object "does not contain enough information to unambiguously locate itself relative to other date/time objects." An aware object "represents a specific moment in time that is not open to interpretation." A datetime is aware if d.utcoffset() does not return None.

datetime.now() with no argument is naive: it is the wall-clock time on the machine, with nothing to say which zone that is. That is fine for a script that prints a timestamp on your own computer. It is a problem when the value is stored, sent to another system, or compared with a time from somewhere else, because the same naive value means different moments in different places. The practical rule: if a timestamp leaves the process, make it aware.

Python also refuses to compare the two kinds. Subtracting or comparing a naive and an aware datetime raises TypeError: can't subtract offset-naive and offset-aware datetimes (or the matching message for comparison), so mixing them fails loudly instead of giving a wrong answer.

How to get the current time in UTC

The recommended way, in the words of the docs, is "calling datetime.now(timezone.utc)". On Python 3.11 and later there is an alias, datetime.UTC, which is the same object as timezone.utc:

from datetime import datetime, timezone, UTC   # UTC needs Python 3.11+

datetime.now(timezone.utc)   # works on every Python 3 version since 3.2
datetime.now(UTC)            # same result, Python 3.11+

The timezone class has been in the standard library since Python 3.2, so timezone.utc is the safer choice if your code has to run on older interpreters.

Why utcnow() is deprecated

Older tutorials use datetime.utcnow(). It returns the current UTC time but with tzinfo set to None, so the object is naive even though the numbers are UTC. The docs explain the danger: "naive datetime objects are treated by many datetime methods as local times". Calling .timestamp() on such a value, for example, assumes the machine's local zone and gives a wrong epoch time on any computer that is not set to UTC.

utcnow() is marked "Deprecated since version 3.12", with the instruction to use datetime.now() with UTC. The same applies to datetime.utcfromtimestamp(), where the docs point to datetime.fromtimestamp(timestamp, tz=timezone.utc). The old functions still work, but newer Python versions can emit a DeprecationWarning when you call them, so replace them when you touch the code:

# before
stamp = datetime.utcnow()

# after
stamp = datetime.now(timezone.utc)

How to get the current time in a specific time zone

Pass a ZoneInfo object from the zoneinfo module (Python 3.9+) as the tz argument. It uses the IANA time zone names and handles daylight saving time for you:

from datetime import datetime
from zoneinfo import ZoneInfo

datetime.now(ZoneInfo("America/New_York"))
datetime.now(ZoneInfo("Europe/Vilnius"))
datetime.now(ZoneInfo("Asia/Tokyo"))

The zoneinfo page documents attaching a zone through the constructor, replace or astimezone; passing it to now() follows from the tz parameter described above.

To convert a time you already have, use astimezone(). It returns the same moment expressed in the new zone:

utc_now = datetime.now(timezone.utc)
utc_now.astimezone(ZoneInfo("America/New_York"))

Be careful with replace(tzinfo=...). It attaches a zone without changing the clock values, so it relabels the time instead of converting it. The docs say to use it only when you "merely want to attach a timezone object". Also, astimezone() on a naive value assumes the system time zone, which is rarely what you want on a server.

ZoneInfoNotFoundError on Windows

Windows has no built-in IANA database. The documentation notes that "some systems, including notably Windows systems, do not have an IANA database available", and that if neither system data nor the tzdata package is present, "all calls to ZoneInfo will raise ZoneInfoNotFoundError". The fix is to install the package from PyPI:

pip install tzdata

The error is a subclass of KeyError. A badly formed key, such as one with path-like characters, raises ValueError instead.

How to format the current time as a string

For a machine-readable timestamp, use isoformat(). It gives ISO 8601 and appends the UTC offset when the datetime is aware:

datetime.now(timezone.utc).isoformat()
# '2026-11-18T12:05:09.123456+00:00'   (example output)

datetime.now(timezone.utc).isoformat(timespec="seconds")
# '2026-11-18T12:05:09+00:00'          (example output)

For text a person will read, use strftime with format codes, for instance datetime.now().strftime("%Y-%m-%d %H:%M:%S"). The codes, the leading-zero differences between Windows and Linux, and the locale traps are covered in Python strftime format codes.

datetime.now() vs today() vs time.time()

CallReturnsTime zoneUse it for
datetime.now()datetimelocal, naiveLocal display on one machine
datetime.now(timezone.utc)datetimeUTC, awareStored or exchanged timestamps
datetime.today()datetimelocal, naiveSame as now(); docs prefer now()
datetime.utcnow()datetimeUTC values, naiveNothing; deprecated since 3.12
time.time()float, secondsnone (epoch)Unix timestamps
time.monotonic()float, secondsnoneMeasuring durations

On today(), the documentation says it is "functionally equivalent to now(), but without a tz parameter", and that now() "is preferred over today() and utcnow()".

Unix timestamps

time.time() returns "the time in seconds since the epoch as a floating-point number", commonly called Unix time. time.time_ns() gives the same as an integer number of nanoseconds (added in Python 3.7). From a datetime, call .timestamp(). For an aware datetime, the result is computed against datetime(1970, 1, 1, tzinfo=timezone.utc), so it is exact. For a naive one, Python assumes local time and relies on platform C functions.

datetime.now(timezone.utc).timestamp()   # e.g. 1794139509.123456
datetime.fromtimestamp(1794139509, tz=timezone.utc)

Passing tz=timezone.utc to fromtimestamp() matters: without it, you get a naive datetime in the machine's local time.

Measuring elapsed time: do not subtract two now() calls

The wall clock can be adjusted while your program runs, by a network time sync or by a daylight-saving change on a naive local time. A difference between two datetime.now() values can therefore be wrong, or even negative. For durations, time.monotonic() returns "a clock that cannot go backwards" and "is not affected by system clock updates". Only the difference between two calls is meaningful, because the reference point is undefined:

import time

start = time.monotonic()
# ... work ...
elapsed = time.monotonic() - start   # seconds, as a float

Testing code that calls datetime.now()

Code that calls datetime.now() internally is hard to test because the answer changes on every run. The simplest fix is to pass the current time in as an argument with a default, so tests can supply a fixed value:

from datetime import datetime, timezone

def is_expired(expires_at: datetime, now: datetime | None = None) -> bool:
    current = now or datetime.now(timezone.utc)
    return expires_at <= current

This also keeps the comparison between two aware values, which avoids the naive-versus-aware TypeError described above.

If the time looks wrong

  • The hour is off by your UTC offset. You are probably comparing a local naive value with a UTC one, or you called utcnow() and then treated it as local. Switch both sides to datetime.now(timezone.utc).
  • No +00:00 in the output. The datetime is naive. Pass timezone.utc or a ZoneInfo to now().
  • The time is correct for the server but wrong for users. Store UTC, then convert with astimezone(ZoneInfo(...)) only when you display it.
  • The machine's clock itself is wrong. datetime.now() reads the operating system's clock, so fix the system time or time zone setting first.

Frequently asked questions

What is the difference between datetime.now() and datetime.today()?
Both return the current local date and time as a naive datetime. The Python documentation says today() is functionally equivalent to now() without a tz parameter, and that now() is preferred. now() can also take a tz argument, which today() cannot.
Is datetime.utcnow() removed?
No, but it has been deprecated since Python 3.12. It returns a naive datetime that looks like UTC but carries no time zone. The documented replacement is datetime.now(timezone.utc), or datetime.now(UTC) on Python 3.11 and later.
How do I get the current date only, without the time?
Call datetime.now().date(), or use datetime.date.today(). Both return a date object with year, month and day. For the date in a specific zone, call datetime.now(tz).date() so the date matches that zone and not the machine's local one.
How do I measure how long something takes?
Use time.monotonic() or time.perf_counter(), not datetime.now(). A monotonic clock cannot go backwards and is not affected by system clock updates, so the difference between two calls is a reliable duration. Wall-clock time can jump when the system clock is adjusted.

Sources