0
votes

I need to convert the date from 12 hour format to 24 hour format.

Input: 01/27/2016 07:01:36 PM

Expected output: 201601271901(YYYYMMDDHHMM)

I have used format-dateTime() function in my code ,I am getting error

<xsl:value-of select="format-dateTime(part_need/promised_dt,'[Y0001][M01][D01][H01][m01]')"/>

Error:

Description: FORG0001: Invalid dateTime value "01/27/2016 07:01:36 PM" (Non-numeric year component)

Please help on this issue

2

2 Answers

0
votes

Your input is not a valid ISO 8601 date/time, so you cannot use the built-in date/time functions on it.

Try instead something like (XSLT 2.0):

<xsl:template match="inputdate">
    <xsl:copy>
        <xsl:variable name="dte" select="tokenize(.,'/|\s|:')" />
        <xsl:value-of select="$dte[3]" />
        <xsl:value-of select="$dte[1]" />
        <xsl:value-of select="$dte[2]" />
        <xsl:variable name="h24" select="xs:integer($dte[4]) mod 12 + 12 * xs:integer($dte[7]='PM')" />
        <xsl:value-of select="format-number($h24, '00')" />
        <xsl:value-of select="$dte[5]" />
    </xsl:copy>
</xsl:template>

Note that this assumes your days are zero-padded to two digits (as are your months).

If you need to use this in several places, consider turning it into a function.

0
votes

format-dateTime takes an xs:dateTime? as the first parameter. The part_needed/promised_dt is a node.

If you have the date-time in the standard ISO format (e.g. "2006-01-27T19:01:36"), you can use xs:dateTime(part_needed/promised_dt).

Saxon does not have a non-standard date-time parser helper, so you would need to use the xs:dateTime(xs:date(year,month,day), xs:time(hours, minutes, seconds)) constructor and use something like substring(part_needed/promised_dt,1,2) to get each date/time part.