Python3 日期和时间

Aware vs naive datetimes, parsing, formatting, and timestamp conversion.

Intermediate
Key PointsStore UTC, convert at the edges; a naive datetime is a bug waiting to happen.

Now and Construct

Python
from datetime import datetime, date, timedelta, timezone

now = datetime.now()                            # naive,本机时区,不推荐入库
utc = datetime.now(timezone.utc)                # aware,推荐
today = date.today()

dt = datetime(2026, 10, 31, 9, 30, 0)

Aware vs Naive

An aware datetime carries a tzinfo, so arithmetic across DST and conversions are correct. A naive datetime has no idea where it is — comparing the two raises TypeError.

Python
from datetime import datetime, timezone, timedelta

cst = timezone(timedelta(hours=8))
local = datetime.now(cst)
print(local.utcoffset())            # 8:00:00

print(local.astimezone(timezone.utc))   # 转到 UTC

# 从数据库取出的 naive 时间,补上时区
stored = datetime(2026, 10, 31, 1, 30)
aware = stored.replace(tzinfo=timezone.utc)

Format and Parse

CodeMeaningExample
%Y4-digit year2026
%mMonth 01-1210
%dDay 01-3131
%H:%M:%S24h time09:30:00
%fMicroseconds000000
%z / %ZUTC offset / zone name+0800 / CST
Python
dt = datetime(2026, 10, 31, 9, 30)
print(dt.strftime("%Y-%m-%d %H:%M"))       # 2026-10-31 09:30
print(dt.isoformat())                      # 2026-10-31T09:30:00

parsed = datetime.strptime("2026-10-31 09:30", "%Y-%m-%d %H:%M")
print(parsed.year)                         # 2026

Arithmetic and Timestamps

Python
from datetime import datetime, timedelta, timezone

start = datetime(2026, 10, 1, tzinfo=timezone.utc)
end = start + timedelta(days=30, hours=2)
print(end - start)                # 30 days, 2:00:00
print((end - start).total_seconds() / 86400)   # 30.083...

ts = end.timestamp()                       # 秒(float)
print(datetime.fromtimestamp(ts, timezone.utc))
Never do date math by adding 86400 seconds — DST days are 23 or 25 hours. Use timedelta(days=1) on aware datetimes.

Friends & Partners