python
deprecation_warning
ai_generated
true
DeprecationWarning: datetime.datetime.utcnow() is deprecated
ID: python/deprecationwarning-datetime-utcnow
98%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
datetime.utcnow() deprecated in Python 3.12+. Returns naive datetime which causes timezone bugs.
genericWorkarounds
-
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
-
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:
-
Suppress the deprecation warning
80% fail
The function will be removed — fix it now
-
Use datetime.now() without timezone
70% fail
Returns local time, not UTC — different behavior
Error Chain
Frequently confused with: