First of all, although your use case is rather simple: generally avoid tampering with dates (and especially times) on your own, and always rely on already implemented functionality, handling date and time is horribly complicated if you want to do it right.
Convert the string to a representation that XQuery's date functions are able to parse. If you can construct a date from the string, it is valid; if an error occurs, it is invalid. Dates can be constructed using xs:date($string)
. One way to convert the string would be using using substring(...)
and string-join(...)
:
xs:date(string-join((substring($date, 1, 4), substring($date, 5, 2), substring($date, 7, 2)), '-'))
To catch an error, use a try
/catch
block.