Using Arithmetic Operators on Dates, Times, and Durations

The arithmetic operations performed on date and time types fall into five categories, described in the following sections:

  1. Subtracting a date or time from another date or time to determine, for example, the elapsed time between 1:32 P.M. and 4:53 P.M.

  2. Adding or subtracting durations from dates to determine, for example, what date is 30 days prior to April 15.

  3. Adding or subtracting two durations to obtain a third duration, which might be used to extend a time period by 30 days.

  4. Multiplying or dividing a duration by decimal numbers to obtain a third duration, which might be used to double a time period or to convert a numeric value to a duration.

  5. Dividing a duration by another duration, which might be used to calculate the ratio of two durations or to convert a duration value to a number.

In addition to these operations, it is also possible to use the aggregation functions (max, min, avg, and sum) on sequences of duration values. You can also use the max and min functions on dates, times, and date/time values.

Subtracting Dates and Times

A value of type xs:date, xs:time, or xs:dateTime can be subtracted from another value of the same type. This is useful for determining the elapsed duration between two points in time. For example, if you want to calculate the response time on a customer order, you can subtract the order date from the delivery date.

The result is negative if the second date or time occurs later in time than the ...

Get XQuery now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.