Python datetime - aware UTC timestamps, ISO 8601 parsing and timedelta math
Answer: Get the current UTC time as an aware object with datetime.now(timezone.utc). fromisoformat() returns a datetime from a date_string in valid ISO 8601 format, and a timedelta represents a duration.
Examples
from datetime import datetime, timedelta, timezone, UTC
now = datetime.now(timezone.utc) # aware
also_now = datetime.now(UTC) # UTC alias, Python 3.11+
ts = datetime.fromisoformat("2026-11-18T10:30:00+00:00")
local = ts.astimezone() # system local time zone
later = ts + timedelta(days=1, hours=2)
print(later.isoformat())
parsed = datetime.strptime("2026-11-18", "%Y-%m-%d") # naive
Aware versus naive
- An aware object represents a specific moment in time that is not open to interpretation.
- A naive object does not contain enough information to unambiguously locate itself; whether it means UTC or local time is purely up to the program.
- Many datetime methods treat naive datetimes as local times, so it is preferred to use aware datetimes to represent times in UTC.
datetime.utcnow()returns a naive datetime and is deprecated since version 3.12; usedatetime.now()with UTC instead.
Parsing and formatting
fromisoformat()accepts any valid ISO 8601 format, with exceptions: reduced precision dates (YYYY-MM), extended dates and ordinal dates are not supported, nor are fractional hours and minutes.strptime(date_string, format)parses according to a format; a%dwithout a year raisesValueError, so always include a year in your format.
timedelta and conversion
- A timedelta object represents a duration, the difference between two datetime or date instances. All arguments default to 0, and only days, seconds and microseconds are stored internally.
astimezone(tz)returns the same UTC time as self, adjusted to the local time of tz. On a naive datetime, it presumes the system time zone.
See also
Claims
- id: C1 claim: Prefer aware datetimes to represent times in UTC. source: S1 evidence: "it is preferred to use aware datetimes to represent times in UTC."
- id: C2 claim: utcnow is deprecated since Python 3.12. source: S1 evidence: "Deprecated since version 3.12: Use datetime.now() with UTC instead."
- id: C3 claim: strptime parses a string according to a format. source: S2 evidence: "Parse string according to the given date format (like time.strptime())."
Sources
- [S1] datetime - Basic date and time types. Python docs. https://docs.python.org/3/library/datetime.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/_pydatetime.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/_pydatetime.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, datetime - Basic date and time types; python/cpython (GitHub), Lib/_pydatetime.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, datetime - Basic date and time types; python/cpython (GitHub), Lib/_pydatetime.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python datetime - aware UTC timestamps, ISO 8601 parsing and timedelta math." iinventedlight Library, iil:note:p9qmt0p5, version of 2026-11-18. https://iinventedlight.com/library/notes/python-datetime/. License: CC-BY-4.0.
@misc{iil_note_p9qmt0p5,
author = {{iinventedlight}},
title = {{Python datetime - aware UTC timestamps, ISO 8601 parsing and timedelta math}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-datetime/},
note = {Permanent ID iil:note:p9qmt0p5. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-datetime.table.json (table; CC-BY-4.0)
- python-datetime.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-datetime.md.