ISO 8601 is the ‘official’ way to represent dates, times, time intervals, and durations.

ISO 8601 is the ‘official’ way to represent dates, weeks, months, and time intervals. For each, there is a “human-readable” version and a “compact” version.

General rules:

  • Granularity increases from left to right, that is to say the representation of the least granular element (e.g. “years”) are on the left, then granularity increases as you move toward the most granular element (e.g. “fractions of a second”) on the right.
  • Leading zeros always included

Dates

These will go in human-readable, then compact-form order.

  • Year
    • YYYY (e.g. 2021)
  • Year & Month
    • YYYY-MM (e.g. 2021-02)
  • Year & Week
    • YYYY-Www (e.g. 2021-W06)
      • Note: week numbers going across year-boundaries count based on the majority of days of that week. In short, you’re counting Thursdays in the year. So, if the year starts on a Friday, January 1st of that year would be labeled as part of the last week of the previous year. (e.g. “Fri 1 Jan 2010” in this format is 2009-W53”)
    • YYYYWww (e.g. 2021W06)
  • Full Date
    • YYYY-MM-DD (e.g. 2021-02-08)
    • YYYYMMDD (e.g. 20210208)

Time

Times get into the realm of time zones & UTC. Time strings are prefixed with the literal character “T” and postfixed with some sort of time zone indicator. In all cases, you can leave off more granular details if they don’t apply.

  • Times (without dates)
    • hh:mm:ss.sss (e.g. 13:50:05.000) ← Implied local time
    • hh:mm:ss.sssZ (e.g. 19:50:05.000Z) ← for 7:50pm UTC time
    • hh:mm:ss.sss±hh:mm (e.g. 13:50:05.000-06:00) ← 1:50pm Central Standard Time
    • Thhmmss.sss (e.g. T135005.000)
    • Thhmmss (e.g. T135005)
    • ThhmmssZ (e.g. T195005Z)
    • Thhmmss±hhmm (e.g. T135005-0600)
    • Thhmmss±hh (e.g. T135005-06)

Full Timestamps

Full timestamps take any valid full date, slap on a “T”, then include any valid time format. There’s a litany of permutations. Here are some examples.

  • YYYY-MM-DDThh:mm:ss.sssZ (e.g. 2021-02-08T13:50:05.000Z)
  • YYYYMMDDThhmmss±hhmm (e.g. 20210208135005-0600)

Durations & Intervals

Durations are prefixed with a “P” (indicating “period”). They can be marked by a combination of an integer number of Years, Months, Weeks, Days, Hours, Minutes, and Seconds (Y,M,W,D,H,M,S, respectively). For symmetry (& to diambiguate “months” and “minutes”) with the Date & Time representation, there is a T separating the “years/months/days” part from the “hours/minutes/seconds” part.

Examples:

  • P5W is a duration of 5 weeks
  • P6M is a duration of 6 months
  • P3Y2M5DT6H33M21S is a duration of 3 years, 2 months, 5 days, 6 hours, 33 minutes, and 21 seconds.

Intervals can be created by specifying a start and end time, or by specifying a start OR end time and the duration of the interval. Regardless of which variety of pair you supply, they are separated with a forward slash ”/“.

Examples:

  • start/end (e.g. 2020-02-08T13:50:05Z/2020-02-08T14:00:00Z)
  • start/duration (e.g. 2020-02-08T13:50:05Z/PT9M55S)
  • duration/end (e.g. PT9M55S/2020-02-08T13:50:05Z)

Repeating Intervals

For repeating intervals, you include a prefixed “R[n]/”, where “[n]” is replaced with the number of repetitions. If no “[n]” is supplied, repetitions go on indefinitely.

Examples:

  • R30/P1D - every day, for thirty days
  • R/5Y3M - every 5 years and 3 months, indefinitely

Other Art

From XKCD:


Source

ISO 8601

ISO 8601