Reputation: 39
I am using XSLT 1.0 and I have a time value stored as an integer in military time and I need to output it into standard time. For example, a value would be 1400 and I need to output it to 2:00 PM. Can this be achieved in XSLT 1.0?
Upvotes: 2
Views: 422
Reputation: 116992
That's more than just formatting. You also want to convert 24-hour notation to 12-hour notation.
Given an input of:
XML
<input>1435</input>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<output>
<xsl:variable name="h" select="input div 100"/>
<xsl:variable name="m" select="input mod 100"/>
<xsl:variable name="h12" select="round(($h + 11) mod 12 + 1)"/>
<xsl:variable name="am.pm" select="substring('AMPM', 1 + 2*($h > 11), 2)"/>
<xsl:value-of select="$h12"/>
<xsl:text>:</xsl:text>
<xsl:value-of select="format-number($m, '00')"/>
<xsl:text> </xsl:text>
<xsl:value-of select="$am.pm"/>
</output>
</xsl:template>
</xsl:stylesheet>
will return:
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>2:35 PM</output>
Upvotes: 3