ISO 8601 Duration Parser & Builder

Parse ISO 8601 durations (P3Y2M10DT2H30M15S) and build canonical strings from numeric inputs.

Open tool

Overview

The ISO 8601 Duration Parser and Builder is a two-way converter for the PnYnMnDTnHnMnS duration format. Paste an ISO string to see its component parts in a labelled table, or fill in the numeric fields to assemble a canonical duration string ready to drop into XML, JSON Schema, or a REST API payload.

Useful for API integrators who need to build a cache-duration field exactly the way the spec demands, schema authors validating duration regexes, and developers debugging why their PT0H30M0S does not parse cleanly in some library because canonical form omits zero components.

How it works

The parser follows the ISO 8601-1:2019 grammar. A valid duration begins with P, optionally lists Y/M/W/D in any order they appear, then a T separator if any time fields follow, and finally H/M/S. The letter M means months before T and minutes after — the position is what disambiguates. Fractional values are permitted on the final present component using . or ,.

The builder emits canonical strings by omitting fields whose value is zero (except when all values are zero, in which case the output is PT0S), placing the T separator only when at least one time component is present, and using . as the decimal mark. That canonical form round-trips cleanly through every major library tested.

Examples

Parse: P3Y2M10DT2H30M15S
→ 3 years, 2 months, 10 days, 2 hours, 30 minutes, 15 seconds

Build: 1 day, 12 hours
→ P1DT12H

Build: 90 minutes
→ PT1H30M (canonical) or PT90M (non-canonical but valid)

Parse: -PT0.5H
→ Minus 30 minutes

FAQ

Why does the builder collapse 90 minutes into 1H30M?

The canonical form prefers the largest plausible unit, since some validators that compare two durations textually would treat PT90M and PT1H30M as different strings even though they encode the same length.

Does it support the W (weeks) form?

Yes for parsing. The builder offers a "weeks" mode that emits PnW exclusively, since the spec forbids mixing weeks with other components.

What is the difference between this tool and the simpler parser?

The simpler tool is read-only. This one round-trips both ways so you can construct exact strings programmatically.

Are negative durations supported?

Yes — the 2019 revision allows a leading minus (-PT1H30M = "minus 90 minutes"). Some legacy parsers (pre-2019 XSD, early Java Duration) will reject it.

How are fractional seconds emitted?

Fractional seconds are written as a decimal with up to nine digits after the point (matching nanosecond precision). Trailing zeros are stripped, so 0.500 becomes 0.5.

Try ISO 8601 Duration Parser & Builder

An unhandled error has occurred. Reload ×