ISO 8601日期格式:国际标准

What Is ISO 8601?

ISO 8601 is an international standard for representing date and time data, published by the International Organization for Standardization. It defines a clear, unambiguous way to write dates that eliminates confusion between formats like MM/DD/YYYY (US) and DD/MM/YYYY (UK). The standard uses a big-endian approach: year → month → day → hour → minute → second.

Date Formats

2024-03-01           # Calendar date (YYYY-MM-DD) — most common
2024-061             # Ordinal date (YYYY-DDD) — day 61 of 2024
2024-W09-5           # Week date (YYYY-Www-D) — Friday of week 9

Time Formats

14:30:00             # Local time (HH:MM:SS)
14:30:00.456         # With fractional seconds
14:30:00Z            # UTC (Z = Zulu = UTC)
14:30:00+09:00       # With UTC offset (e.g., Korea Standard Time)
14:30:00-05:00       # Negative offset (e.g., EST)

Combined Date-Time (Most Common in APIs)

2024-03-01T14:30:00Z          # UTC datetime (preferred)
2024-03-01T14:30:00+09:00     # KST datetime
2024-03-01T14:30:00.456Z      # With milliseconds
2024-03-01T14:30:00.456789Z   # With microseconds

Duration Format

ISO 8601 also defines duration notation with the prefix P:

P1Y2M3DT4H5M6S    # 1 year, 2 months, 3 days, 4 hours, 5 minutes, 6 seconds
P30D              # 30 days
PT1H30M           # 1 hour 30 minutes (time-only duration starts with PT)

Interval Format

2024-03-01T00:00:00Z/2024-03-31T23:59:59Z   # Start/End
2024-03-01T00:00:00Z/P1M                    # Start/Duration
P1M/2024-03-31T23:59:59Z                    # Duration/End

Why Use ISO 8601?

  • Unambiguous: 03/04/05 could be March 4 2005, April 3 2005, or May 4 2003. ISO 8601 has only one interpretation.
  • Sortable: ISO 8601 strings sort lexicographically in chronological order — critical for log files and databases.
  • Machine-readable: Every major programming language can parse ISO 8601 natively.
  • Internationally understood: Avoids the MM/DD vs DD/MM confusion between US and European conventions.

Parsing in Code

# Python
from datetime import datetime
dt = datetime.fromisoformat("2024-03-01T14:30:00+09:00")

# JavaScript
new Date("2024-03-01T14:30:00Z")

# Go
time.Parse(time.RFC3339, "2024-03-01T14:30:00Z")