Hi Christian,

Much appreciated! I hardened the code (see below) since the dates (from
CT.gov) occasionally also have the day of the month (e.g., “March 21,
2014”). Currently the function is dropping the day in such cases but I will
look into capturing it in a future iteration.

Best,
Ron


declare function local:to-date($string) {
  if (fn:matches($string, '[A-Za-z]+ [0-9]+') or fn:matches($string,
'[A-Za-z]+ [0-9]+, [0-9]+'))
  then
  let $m := index-of($MONTHS, substring-before($string, ' '))
  let $y := xs:integer(functx:substring-after-last($string, ' '))
  return xs:date(string-join(
    (
      format-number($y, '0000'),
      format-number($m, '00'),
      '01'
    ),
    '-')
  )
  else
  ()
};

On November 2, 2018 at 4:20:41 PM, Christian Grün (christian.gr...@gmail.com)
wrote:

Hi Ron,

If your timestamp is available in IETF format, you can use
fn:parse-ietf-date [1]. Otherwise, you’ll need to write a simple
function by yourself:

declare variable $MONTHS := (
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
);

declare function local:to-date($string) {
let $m := index-of($MONTHS, substring-before($string, ' '))
let $y := xs:integer(substring-after($string, ' '))
return xs:date(string-join((
format-number($y, '0000'),
format-number($m, '00'),
'01'
), '-'))
};
local:to-date('March 2017')

Best,
Christian

[1]
https://urldefense.proofpoint.com/v2/url?u=https-3A__www.w3.org_TR_xpath-2Dfunctions-2D31_-23func-2Dparse-2Dietf-2Ddate&d=DwIFaQ&c=fi2D4-9xMzmjyjREwHYlAw&r=44jDQvzmnB_-ovfO6Iusj0ItciJrcWMOQQwd2peEBBE&m=Yy9sC1xS_Ix-pGSVDp-Lbmz8BOft0S1WdVpEM-qzRw4&s=uQ3NqCv8FpHulP4q1arjItJX3-gCHwi_06WN4znRz48&e=



On Fri, Nov 2, 2018 at 9:09 PM Ron Katriel <rkatr...@mdsol.com> wrote:
>
> Hi,
>
> Is there a BaseX function for converting a string date in the form of
“March 2017” to xs:date or xs:dateTime?
>
> Thanks,
> Ron

Reply via email to