Need to get the difference in milliseconds between two different timestamps using XQuery in MarkLogic

Viewed 26

I need to add milliseconds into the timestamp and need to get the difference in milliseconds.

My Code:

xquery version "1.0-ml";
let $date1 := xs:dateTime("2022-01-17T21:45:00")
let $date2 := xs:dateTime("2022-01-17T22:45:00")
let $result := fn:abs(($date1 - $date2) div xs:dayTimeDuration("PT1S"))
return $result * 1000 (:Convert sec to ms:)
1 Answers

If you want the duration presented in milliseconds, then divide by xs:dayTimeDuration('PT0.001S')

($date1 - $date2) div xs:dayTimeDuration('PT0.001S')

and if you want it as a positive number, then apply abs() as you were before:

abs(($date1 - $date2) div xs:dayTimeDuration('PT0.001S'))
Related