
XQuery Time & Date
In XQuery, you can work with time and date values using the built-in functions and data types provided by the language. XQuery supports the xs:time, xs:date, and xs:dateTime data types to represent time, date, and date-time values, respectively.
Here are the main XQuery functions related to time and date:
Current Time and Date Functions:
fn:current-time()
- Returns the current time as an
xs:timevalue.
fn:current-date()
- Returns the current date as an
xs:datevalue.
fn:current-dateTime()
- Returns the current date and time as an
xs:dateTimevalue.
Extracting Components from Date-Time Values:
fn:year-from-dateTime($dateTime)
- Extracts the year from an
xs:dateTimevalue.
fn:month-from-dateTime($dateTime)
- Extracts the month from an
xs:dateTimevalue.
fn:day-from-dateTime($dateTime)
- Extracts the day from an
xs:dateTimevalue.
fn:hours-from-time($time)
- Extracts the hours from an
xs:timevalue.
fn:minutes-from-time($time)
- Extracts the minutes from an
xs:timevalue.
fn:seconds-from-time($time)
- Extracts the seconds from an
xs:timevalue.
Formatting Dates and Times:
fn:format-dateTime($dateTime, $picture, $language, $calendar)
- Formats an
xs:dateTimevalue as a string using the specified picture string.
fn:format-date($date, $picture, $language, $calendar)
- Formats an
xs:datevalue as a string using the specified picture string.
fn:format-time($time, $picture, $language, $calendar)
- Formats an
xs:timevalue as a string using the specified picture string.
These functions enable you to work with time and date values, extract components from date-time values, and format date-time values as strings based on a specified picture string.
Here’s an example of using some of these functions:
let $currentDateTime := fn:current-dateTime()
let $year := fn:year-from-dateTime($currentDateTime)
let $formattedDate := fn:format-dateTime($currentDateTime, "[Y]-[M01]-[D01] [H01]:[m01]:[s01]")
return $formattedDate
Output (assuming the current date and time is 2023-07-27T14:30:45):
2023-07-27 14:30:45
Remember that the output of these functions may vary based on the system’s clock and time zone settings. Always consider the timezone context while working with date and time values in XQuery.