duration vs yearMonthDuration vs dayTimeDuration in XML Schema
In XML Schema (W3C XSD), managing elapsed time involves three
distinct data types: xs:duration,
xs:yearMonthDuration, and xs:dayTimeDuration.
While all three represent time spans rather than specific points in
time, they differ significantly in the time components they permit,
their standard format, and whether they can be deterministically
compared and ordered against one another.
xs:duration
xs:duration is the universal base data type for all time
intervals in XML Schema. It represents an interval of time defined by
both calendar components (years and months) and clock components (days,
hours, minutes, and seconds).
- Format: Follows the ISO 8601 format
PnYnMnDTnHnMnS(e.g.,P1Y2M3DT4H5M6Srepresents 1 year, 2 months, 3 days, 4 hours, 5 minutes, and 6 seconds). - Ordering Limitation: Values of
xs:durationare only partially ordered. Because the length of a month or year varies depending on the calendar context (e.g., a month can be 28, 29, 30, or 31 days), direct mathematical comparisons between calendar and clock intervals cannot always be definitively resolved without a starting reference point. For instance, it is impossible to definitively determine whetherP1M(1 month) is greater than, equal to, or less thanP30D(30 days).
xs:yearMonthDuration
xs:yearMonthDuration is a derived restriction of
xs:duration that includes exclusively calendar-based year
and month components. All day and time components are prohibited.
- Format: Uses the structure
PnYnM(e.g.,P2Y6Mfor 2 years and 6 months, orP5Yfor 5 years). - Ordering: Values are totally
ordered. Since a year always consists of exactly 12 months, any
xs:yearMonthDurationcan be normalized into a total number of months, enabling precise comparison and arithmetic operations (e.g.,P1Yis always strictly greater thanP11M).
xs:dayTimeDuration
xs:dayTimeDuration is a derived restriction of
xs:duration that includes exclusively fixed-length time
units: days, hours, minutes, and seconds. Year and month components are
prohibited.
- Format: Uses the structure
PnDTnHnMnS(e.g.,P3DT12Hfor 3 days and 12 hours, orPT45Sfor 45 seconds). - Ordering: Values are totally ordered. Because days, hours, minutes, and seconds have fixed relative values (1 day = 24 hours, 1 hour = 60 minutes, 1 minute = 60 seconds), any instance can be converted into an exact number of seconds. This allows direct ordering, sorting, and arithmetic comparisons without ambiguity.
Key Differences Summary
| Feature | xs:duration |
xs:yearMonthDuration |
xs:dayTimeDuration |
|---|---|---|---|
| Allowed Units | Years, Months, Days, Hours, Minutes, Seconds | Years, Months only | Days, Hours, Minutes, Seconds only |
| Example Value | P1Y3MT10H |
P1Y3M |
P5DT10H |
| Ordering Property | Partially ordered | Totally ordered | Totally ordered |
| Common Use Case | General durations with mixed calendar/time components | Subscriptions, contractual terms, historical spans | Execution times, timeouts, precise durations |