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).

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.

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.

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