Monday, September 29, 2014

Example for subtracting time using xp20:subtract-dayTimeDuration-from-dateTime


We can use the  xp20:subtract-dayTimeDuration-from-dateTime(String1, String2) function to subtract  duration of String2 from String1 time.

For example:
         To subtract one day
              xp20:subtract-dayTimeDuration-from-dateTime(xp20:current-date(), 'P1D')

         To subtract  2 hours from the current date time 
             xp20:subtract-dayTimeDuration-from-dateTime(xp20:current-date(), 'PT2H')

        To subtract  one day 2 hours 
             xp20:subtract-dayTimeDuration-from-dateTime(xp20:current-date(), 'PD1DT2H')


The following example query adds a dayTimeDuration value equal to 1 day, 2 hours, 30 minutes, and 5 seconds to a date Time value equal to the date: January 1, 2003 and time: 1:00 AM as shown in the following query:
{
op:add-dayTimeDuration-to-dateTime(xs:dateTime("2003-01-01T01:00:00"), xf:dayTimeDuration("P1DT2H30M5S"))
}
The resulting date Time value equal to the date: January 2, 2003 and the time: 3:30:05 AM is returned as shown in the following result:
2003-01-02T03:30:05

Note:  The key observation here is that the format of String2,  if you don't have the time to be subtracted then just use  P, if you have time to be subtracted then user PT at the beginning.