Skip to main content
Dates

How to get the current date and time in Python

Quick answer

datetime.now(UTC) for timezone-aware UTC. datetime.now() (no arg) is naive local time — usually a bug.

from datetime import datetime, UTC

now_utc = datetime.now(UTC)
print(now_utc)   # 2026-08-01 15:30:00.123456+00:00

print(now_utc.isoformat())  # '2026-08-01T15:30:00.123456+00:00'

In Python 3.12+, datetime.now(UTC) is the recommended way to get a timezone-aware UTC timestamp. datetime.utcnow() (the old name) is deprecated because it returns a NAIVE datetime — no timezone info attached — which silently causes bugs the moment it interacts with anything aware.

Rule of thumb: store and pass around UTC-aware datetimes everywhere. Only convert to local time at the very last moment when displaying to a user. That way DST transitions and timezone changes never touch your logic.

For named zones like 'Europe/Berlin' use zoneinfo (stdlib since 3.9): ZoneInfo('Europe/Berlin'). It replaces the third-party pytz that older code depends on.

Variations

Current UTC (all Python 3 versions)

from datetime import datetime, timezone
now = datetime.now(timezone.utc)

Works on 3.9+. UTC (imported from datetime) is a shortcut added in 3.11.

Current time in a named timezone

from datetime import datetime
from zoneinfo import ZoneInfo
print(datetime.now(ZoneInfo("Europe/Berlin")))

zoneinfo is stdlib since 3.9. Handles DST correctly.

Unix timestamp

import time
print(time.time())  # 1754059800.123

Seconds since epoch — timezone-independent by definition.

Practise this in your browser

Every how-to has a live lesson. Free tier, no card.

Start free →

Unlock every lesson

Pro — $12/mo, unlimited AI, cancel any time.

See Pro plans →

Get one Python lesson + one career idea every Friday

No spam, no "buy our course now". Three bullets, every Friday. Unsubscribe with one click.

Related how-tos