1

I have the following string:

input.xml

<Request>
    <ClientDt>1/6/2016 11:15:25 AM</ClientDt>
    <!-- rest of input -->
</Request>

The date section of the string is in M/D/YYYY format. I need to convert it to the format listed in the question title, for example

output.xml

<Request>
    <ClientDt>2016-01-06T11:15:25:000000-05:00</ClientDt>
    <!-- rest of output -->
</Request>

the -05:00 being UTC i.e. this is eastern time, which is 5 hours behind UTC

I initially tried piping the value through the format-dateTime function with the appropriate parameters set:

stylesheet.xsl

<xsl:template match="ClientDt">
    <xsl:value-of select="format-dateTime(., '[Y0001]-[M01]-[D-01]T[H01]:[m01]:[s01]:[f000001]')" />
    <!-- Note: yet to figure out timezone part of this -->
</xsl:template>

but got errors stating that the datetime was invalid(Non-numeric year component). To see if it would work otherwise I changed the date portion to 2016-1-6 and kept the time as is, and it throws an error stating the month must be 2 digits.Again, I tweaked the input date to 2016-01-06 and got a similar error, this time stating day must be 2 digits.

Are there any other standard functions that could deal with this format of dateTime, or any in another library? Or would I have to try and piece this together with various pieces of regex, i.e. add in zeroes to month and day if required, switch the parts of the date around manually via concat, etc? I am using Saxon-He version 9.7.0-8

jbailie1991
  • 1,305
  • 2
  • 21
  • 42
  • You cannot use any function that requires a date or a datetime as its argument, because your input is not in the required format for date or datetime. Since your final result is a valid datetime anyway, you can just use a few text manipulations to get it and be done. Well, except the AM/PM thing, that might be more challenging - see: http://stackoverflow.com/questions/30245131/xslt-1-0-find-the-maximum-value-from-given-date-and-time/30245775#30245775 – michael.hor257k Feb 06 '17 at 12:36
  • ah ok, so manual manipulation it is. The AM/PM section isn't needed, it can be dropped, just wanted to check if there was already a function I could make use of – jbailie1991 Feb 06 '17 at 13:07
  • I don't see how you can drop the AM/PM indicator without converting the hour to 24-hour format, e.g. `11:15:25 PM` --> `23:15:25`. – michael.hor257k Feb 06 '17 at 13:12

0 Answers0