← All notes

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

Parsing and formatting

timedelta and conversion

See also

Claims

Sources

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

Permanent ID: iil:note:p9qmt0p5

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.},
}

Markdown

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.

All artifact files (JSON index)

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.