date.today / datetime.now / utcnow
Three ways to ask "what time is it?" — only one of them gives an unambiguous answer: datetime.now(UTC). utcnow() returns the same numbers without a tzinfo and has been deprecated since Python 3.12.
Demo
from datetime import datetime, timedelta, timezone now = datetime.now(timezone(timedelta(hours=0))) (now.utcoffset(), now.tzname())
Whatever the moment, now(tz) is aware and reports the zone it was given; timezone(timedelta(0)) is timezone.utc, named "UTC". The actual time value is left out on purpose: it changes every call and depends on the machine clock.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| tz | tzinfo | None | no (None) | None: naive local wall time of the machine. A tzinfo (UTC, timezone(...), ZoneInfo): an aware datetime in that zone. |
Return value
datetime — now(): naive local time without tz, aware time in tz with it. date.today(): the local date. utcnow(): naive UTC fields.
Common patterns
from datetime import datetime, UTC created_at = datetime.now(UTC).isoformat()
from datetime import datetime, UTC local = datetime.now(UTC).astimezone()
from datetime import datetime from zoneinfo import ZoneInfo today_in_tokyo = datetime.now(ZoneInfo('Asia/Tokyo')).date()
from datetime import datetime, UTC # before: datetime.utcnow() now = datetime.now(UTC)
Examples
Pitfalls
import warnings from datetime import datetime with warnings.catch_warnings(): warnings.simplefilter("ignore") now = datetime.utcnow() now.tzinfo is None
from datetime import datetime, UTC datetime.now(UTC).tzinfo is UTC
from datetime import datetime datetime.now() < datetime.fromisoformat('2999-01-01T00:00Z')
from datetime import datetime, UTC datetime.now(UTC) < datetime.fromisoformat('2999-01-01T00:00Z')
When to use
- datetime.now(UTC) for anything stored or compared
- date.today() for a local calendar date on a single machine (scripts, reports)
- utcnow() → now(UTC)
- Measuring durations → time.perf_counter() (now() can jump when the clock is adjusted)
- Server code that must know the user's date → now(user_zone).date(), not date.today()
Notes
FAQ
It returns a naive datetime whose fields are UTC. Python treats naive values as local time in methods like timestamp() and astimezone(), so utcnow() results were silently misinterpreted. Since 3.12 it emits a DeprecationWarning; use datetime.now(UTC) (or now(timezone.utc) before 3.11).