python deprecation_warning ai_generated true

DeprecationWarning: datetime.datetime.utcnow() is deprecated

ID: python/deprecationwarning-datetime-utcnow

Also available as: JSON · Markdown
98%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
311 active

Root Cause

datetime.utcnow() deprecated in Python 3.12+. Returns naive datetime which causes timezone bugs.

generic

Workarounds

  1. 98% success Use datetime.now(timezone.utc) instead of utcnow()
    from datetime import datetime, timezone
    now = datetime.now(timezone.utc)

    Sources: https://docs.python.org/3/library/datetime.html#datetime.datetime.now

  2. 95% success Or use datetime.now(tz=timezone.utc) for explicit timezone-aware datetime
    tz=timezone.utc

    Sources: https://docs.python.org/3/library/datetime.html#datetime.datetime.now

Dead Ends

Common approaches that don't work:

  1. Suppress the deprecation warning 80% fail

    The function will be removed — fix it now

  2. Use datetime.now() without timezone 70% fail

    Returns local time, not UTC — different behavior

Error Chain

Frequently confused with: