op:subtract-dates

Computes the time difference between $date-var1 and $date-var2. Subtracts the value of $date-var2 from $date-var1.

If the value of $date-var2 follows in time the value of $date-var1, then the returned value is a negative duration.

If the value of $date-var1 or $date-var2 is the empty sequence, the following error is displayed:

Error occurred while executing XQuery: Error loading the XQuery or XSLT for this method: 
Type error in function subtract-dates invocation: expected type 
[date@http://www.w3.org/2001/XMLSchema], 
given type empty 

Signatures

op:subtract-dates(xs:date $date-var1, xs:date $date-var2) —> xf:dayTimeDuration

Arguments

Data Type
Argument
Description

xs:date

$date-var1

Contains a representation of the date.

xs:date

$date-var2

Contains a representation of the date.

Returns

Returns the time difference between $date-var1 and $date-var2 as dayTimeDuration.

Examples

Positive Difference

The following example query returns a positive number of days because $date-var2 precedes in time $date-var1:

<num_days>{xf:get-days-from-dayTimeDuration(op:subtract-dates(xs:date("2002-08-30"), 
xs:date("2001-08-30")))}</num_days> 

The preceding query generates the following result:

<num_days>365</num_days> 

Negative Difference

The following example query returns a negative number of days because $date-var2 follows in time $date-var1:

<num_days>{xf:get-days-from-dayTimeDuration(op:subtract-dates(xs:date("2001-08-30"), 
xs:date("2002-08-30")))}</num_days> 

The preceding query generates the following result:

<num_days>-365</num_days> 

Related Topics

W3C subtract-dates operator description.

W3C date data type description.

W3C dayTimeDuration operator description.