All articles

ISO 8601 duration: how to write a duration in a standard format

The ISO 8601 standard defines not just a date format, but also a format for writing durations — a string like PT1H30M, which unambiguously and machine-readably describes "1 hour 30 minutes."

The structure of the format

The string always starts with the letter P (period). Date components (years, months, days) come right after P, while time components (hours, minutes, seconds) come after the letter T, which separates the date part from the time part. For example, P2DT4H means "2 days and 4 hours."

Why the letter T is needed

Without the T separator, the letter M would be ambiguous — it stands for both months (in the date part) and minutes (in the time part). So P1M means "1 month," while PT1M means "1 minute."

A format built into a W3C standard, not just APIs

The XML Schema specification defines xs:duration as a built-in datatype using essentially this same ISO 8601 syntax, which is why it shows up across a wide range of XML-based formats — SOAP web services, WSDL contracts, and countless enterprise XML schemas all validate duration fields against this exact grammar, decades before most JSON APIs adopted it too.

Why you'd need this

  • Decoding a video's or event's duration from a third-party API response.
  • Producing a valid ISO 8601 duration string for your own API or data feed.
  • Converting durations between a human-readable format and the machine-readable ISO notation.

Fractional seconds in the notation

The ISO 8601 standard allows a fractional part only in the last (smallest) component of the string — for example, PT1.5S means "1.5 seconds," and PT1H30.5M means "1 hour 30.5 minutes." A fractional part in any component other than the last one is not provided for by the standard.

Try the tool