Datypic Logo
XQuery

XQuery

Priscilla Walmsley (pwalmsley@datypic.com)

ISBN: 1491915102

2nd edition, 2015, O'Reilly Media, Inc.

Chapter 20: Working with Dates, Times, and Durations

Please note that the book contains many inline examples and informal tables that are not provided here.

Table 20-2. Examples of comparing dates and times
ExampleReturn value
xs:time("17:15:20-00:00") = xs:time("12:15:20-05:00") true
xs:time("12:15:20-05:00") > xs:time("12:15:20-04:00") true
xs:time("12:15:20-05:00") = xs:time("12:15:20") true if the implicit time zone is -05:00
xs:dateTime("2015-05-03T05:00:00") = xs:dateTime("2015-05-03T07:00:00") false
xs:date("2015-12-25") < xs:date("2014-01-06") false
xs:time("12:15:20") > xs:time("12:15:30") false
xs:date("2015-05-06") > xs:dateTime("2015-05-03T13:20:00") Error XPTY0004
Table 20-4. Examples of comparing durations
ExampleReturn value
xs:yearMonthDuration("P1Y") = xs:yearMonthDuration("P1Y") true
xs:yearMonthDuration("P1Y") = xs:yearMonthDuration("P12M") true
xs:yearMonthDuration("P1Y2M") > xs:yearMonthDuration("P1Y3M") false
xs:dayTimeDuration("P1DT12H") = xs:dayTimeDuration("PT36H") true
xs:dayTimeDuration("P1DT12H") < xs:dayTimeDuration("PT37H") true
xs:yearMonthDuration("P1Y") < xs:dayTimeDuration("P366D") Error XPTY0004
xs:duration("P11M") < xs:duration("P12M") Error XPTY0004
Table 20-6. Examples of the component extraction functions
ExampleReturn value
month-from-date(xs:date("2015-05-31")) 5
hours-from-time(xs:time("09:54:00")) 9
seconds-from-duration(xs:duration("P5DT1H13.6S")) 13.6
seconds-from-duration(xs:duration("-P5DT1H13.6S")) -13.6
years-from-duration (xs:duration("P5Y3M")) 5
years-from-duration (xs:duration("P1Y15M")) 2
Table 20-7. Formatting dates and times examples
ExampleReturn value
format-date(xs:date("2015-04-06"), "[M01]/[D01]/[Y0001]") 04/06/2015
format-date(xs:date("2015-04-06"), "[M]-[D]-[Y]") 4-6-2015
format-date(xs:date("2015-04-06"), "[MNn] [D], [Y]", "en", (), () ) April 6, 2015
format-date(xs:date("2015-04-06"), "[MNn] [D1o], [Y]", "en", (), () ) April 6th, 2015
format-dateTime(xs:dateTime("2015-04-06T17:31:03"), "[M01]/[D01]/[Y0001] at [H01]:[m01]:[s01]") 04/06/2015 at 17:31:03
format-dateTime(xs:dateTime("2015-04-06T17:31:03"), "[h].[m01][Pn] on [FNn], [MNn] [D1o]") 5.31p.m. on Monday, April 6th
format-dateTime(xs:dateTime("2015-04-06T17:31:03"), "[MNn] [D], [Y]", "en", (), () ) April 6, 2015
format-time(xs:time("17:31:03"), "[h]:[m01] [PN]") 5:31 P.M.
format-time(xs:time("17:31:03"), "[H]:[m01]") 17:31
format-time(xs:time("17:31:03-05:00"), "[h]:[m01]:[s01] [PN] [z]") 5:31:03 P.M. GMT-05:00
format-time(xs:time("17:31:03-05:00"), "[h]:[m01]:[s01] [PN] [ZN,*-3]", "en", (), () ) 5:31:03 P.M. EST
Table 20-8. Examples of subtracting dates and times
ExampleReturn value
xs:dateTime("2015-04-11T09:23:30.5") - xs:dateTime("2015-04-04T02:15:10.2") P7DT7H8M20.3S
xs:dateTime("2015-05-03T12:15:30.5") - xs:dateTime("2015-05-03T12:15:10.2") PT20.3S
xs:date("2015-05-06") - xs:date("2015-05-03") P3D
xs:date("2015-04-02") - xs:date("2014-03-11") P387D
xs:date("2015-05-03") - xs:date("2015-05-03") PT0S
xs:date("2015-05-03") - xs:date("2015-05-06") -P3D
xs:time("13:12:02.001") - xs:time("13:12:00") PT2.001S
xs:time("13:12:00-03:00") - xs:time("13:12:00-05:00") -PT2H
xs:time("08:00:00-05:00") - xs:time("09:00:00-02:00") PT2H
xs:time("13:12:00-03:00") - xs:time("13:12:00") -PT2H (if the implicit time zone is -05:00)
Table 20-9. Examples of adding durations to dates and times
ExampleReturn value
xs:dateTime("2015-05-03T09:12:35") + xs:yearMonthDuration("P1Y2M") 2016-07-03T09:12:35
xs:dateTime("2015-04-29T09:12:35") + xs:dayTimeDuration("P5DT2H12M") 2015-05-04T11:24:35
xs:dateTime("2015-04-29T09:12:35") + xs:dayTimeDuration("P5DT17H12M") 2015-05-05T02:24:35
xs:dateTime("2015-04-29T09:12:35") - xs:yearMonthDuration("P1Y") 2014-04-29T09:12:35
xs:yearMonthDuration("P1Y5M") + xs:date("2015-10-02") 2017-03-02
xs:date("2015-10-02") - xs:dayTimeDuration("PT48H") 2015-09-30
xs:date("2015-03-31") - xs:yearMonthDuration("P1M") 2015-02-28
xs:time("09:12:35") + xs:dayTimeDuration("P5DT2H12M") 11:24:35
xs:time("09:12:35") + xs:yearMonthDuration("P1Y2M") Error XPTY0004
Table 20-10. Examples of adding and subtracting durations
ExampleReturn value
xs:yearMonthDuration("P3Y10M") + xs:yearMonthDuration("P5Y5M") P9Y3M
xs:dayTimeDuration("P2DT14H55.3S") - xs:dayTimeDuration("P1DT12H51.2S") P1DT2H4.1S
xs:dayTimeDuration("P1DT12H51.2S") - xs:dayTimeDuration("P2DT14H55.3S") -P1DT2H4.1S
xs:yearMonthDuration("P3Y10M") + xs:dayTimeDuration("P1DT12H") Error XPTY0004
Table 20-11. Examples of multiplying and dividing durations by numbers
ExampleReturn valueReturn type
xs:yearMonthDuration("P1Y6M") * 3.5 P5Y3M xs:yearMonthDuration
3 * xs:dayTimeDuration("PT50M") PT2H30M xs:dayTimeDuration
xs:yearMonthDuration("P2Y6M") div 2 P1Y3M xs:yearMonthDuration
10 * xs:yearMonthDuration("P1M") P10M xs:yearMonthDuration
Table 20-12. Examples of dividing durations by durations
ExampleReturn value
xs:yearMonthDuration("P1Y") div xs:yearMonthDuration("P6M") 2
xs:dayTimeDuration("PT25M") div xs:dayTimeDuration("PT50M") 0.5
xs:dayTimeDuration("PT2H") div xs:dayTimeDuration("PT1M") 120
Datypic XQuery Services