Skip to main content

Date arithmetic

Maxime avatar
Written by Maxime
Updated over 3 weeks ago

You can do date arithmetic by using XPath operators. Examples :

  • Add 1 day to a date (result : 2010-03-01):
    xs:date('2010-02-28') + xs:dayTimeDuration('P1D')

  • Add 1 month and 2 days to a date (result : 2010-03-30):
    xs:date('2010-02-28') + xs:yearMonthDuration ('P1M') + xs:dayTimeDuration('P2D')

  • Add 2 years to a date (result : 2012-02-28):
    xs:date('2010-02-28') + xs:yearMonthDuration ('P2Y')

  • Subtract 1 month and 2 days to a date (result : 2010-01-26):
    xs:date('2010-02-28') - xs:yearMonthDuration ('P1M') - xs:dayTimeDuration('P2D')

  • Add 1 day, 12 hours, 5 minutes and 20 seconds to a date time (result : 2010-03-02T08:05:20)
    xs:dateTime('2010-02-28T20:00:00') + xs:dayTimeDuration('P1DT12H5M20S')

NOTE: You can just use a data field for both the date and the duration (I used static strings to make the examples more understandable)

Duration syntax

You can add or subtract a xs:yearMonthDuration or a xs:dayTimeDuration from a date. You should use xs:yearMonthDuration to specify years and month. You should use xs:dayTimeDuration to specify days, hours, minutes and seconds. The following two sections give a formal definition of the syntax, but the examples of the above section already speak for themselves.

xs:yearMonthDuration

Definition xs:yearMonthDuration is derived from xs:duration by restricting its lexical representation to contain only the year and month components. The value space of xs:yearMonthDuration is the set of xs:integer month values. The year and month components of xs:yearMonthDuration correspond to the Gregorian year and month components defined in section 5.5.3.2 of ISO 8601, respectively.

The lexical representation for xs:yearMonthDuration is the ISO 8601 reduced format PnYnM, where nY represents the number of years and nM the number of months. The values of the years and months components are not restricted but allow an arbitrary unsigned xs:integer.

An optional preceding minus sign ('-') is allowed to indicate a negative duration. If the sign is omitted a positive duration is indicated. To indicate a xs:yearMonthDuration of 1 year, 2 months, one would write: P1Y2M. One could also indicate a xs:yearMonthDuration of minus 13 months as: -P13M.

Reduced precision and truncated representations of this format are allowed provided they conform to the following:

If the number of years or months in any expression equals zero (0), the number and its corresponding designator - may- be omitted. However, at least one number and its designator - must- be present. For example, P1347Y and P1347M are allowed; P-1347M is not allowed, although -P1347M is allowed. P1Y2MT is not allowed. Also, P24YM is not allowed, nor is PY43M since Y must have at least one preceding digit and M must have one preceding digit.

xs:dayTimeDuration

Definition xs:dayTimeDuration is derived from xs:duration by restricting its lexical representation to contain only the days, hours, minutes and seconds components. The value space of xs:dayTimeDuration is the set of fractional second values. The components of xs:dayTimeDuration correspond to the day, hour, minute and second components defined in Section 5.5.3.2 of ISO 8601, respectively.

The lexical representation for xs:dayTimeDuration is the ISO 8601 truncated format PnDTnHnMnS, where nD represents the number of days, T is the date/time separator, nH the number of hours, nM the number of minutes and nS the number of seconds.

The values of the days, hours and minutes components are not restricted, but allow an arbitrary unsigned xs:integer. Similarly, the value of the seconds component allows an arbitrary unsigned xs:decimal. An optional minus sign ('-') is allowed to precede the 'P', indicating a negative duration. If the sign is omitted, the duration is positive. See also ISO 8601 Date and Time Formats.

For example, to indicate a duration of 3 days, 10 hours and 30 minutes, one would write: P3DT10H30M. One could also indicate a duration of minus 120 days as: -P120D. Reduced precision and truncated representations of this format are allowed, provided they conform to the following:

  • If the number of days, hours, minutes, or seconds in any expression equals zero (0), the number and its corresponding designator - may- be omitted. However, at least one number and its designator - must- be present.

  • The seconds part - may- have a decimal fraction.
    *The designator 'T' - must- be absent if and only if all of the time items are absent. The designator 'P' - must- always be present.

For example, P13D, PT47H, P3DT2H, -PT35.89S and P4DT251M are all allowed. P-134D is not allowed (invalid location of minus sign), although -P134D is allowed.

Did this answer your question?