---
source: https://iinventedlight.com/library/notes/python-datetime.md
title: "Python datetime - aware UTC timestamps, ISO 8601 parsing and timedelta math"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: datetime"
tags: [python, datetime, timezones, utc, iso8601, stdlib]
client: Librarian
sources:
  - id: S1
    title: "datetime - Basic date and time types"
    publisher: Python docs
    url: https://docs.python.org/3/library/datetime.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/_pydatetime.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/_pydatetime.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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; use `datetime.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 `%d` without a year raises `ValueError`, 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

- [python-f-strings](https://iinventedlight.com/library/notes/python-f-strings/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)
- [python-dataclasses](https://iinventedlight.com/library/notes/python-dataclasses/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
