How to convert relative dates such as Today and Yesterday to an XQuery date object?

XQuery has a set of useful functions for converting dates. But how do you convert relevant dates such as Today and Yesterday to an actual date?

For example, “Today, 17:33” should be converted to “2012-05-30” and “Yesterday, 22:13” to “2012-05-29”.

+5
source share
1 answer

1. Parsing a date string

. , , , . , , , , ! XQuery .

declare function local:from-relative-date($string as xs:string) as xs:date {
    switch (lower-case(substring-before($string, ",")))
        case "today"                return current-date()
        case "yesterday"            return current-date() - xs:dayTimeDuration('P1D')
        case "day before yesterday" return current-date() - 2 * xs:dayTimeDuration('P1D')       
        case "tomorrow"             return current-date() + xs:dayTimeDuration('P1D')       
        case "day after tomorrow"   return current-date() + 2 * xs:dayTimeDuration('P1D')       
        default                     return error(xs:QName("XPTY0004"), "Unknown Date")
};

2.

XQuery 3.0 format-date(...) (, XQuery , BaseX, ), , :

format-date(local:from-relative-date("Yesterday, 22:13"), "[Y]-[M00]-[D00]")

, year-from-date(...) month day, .

+8
source

All Articles