How to convert ticks into a readable datetime with XSLT?

MrFox picture MrFox · Mar 13, 2009 · Viewed 21.1k times · Source

I have an XML with timestamps like this:

<node stamp="1236888746689" />

And I would like to display them in the result HTML as date with time. Is there a way to do it with XSLT (any Version)?

EDIT: I am using XSLT2.0 with Saxon9. The base date is 1970-01-01 0:00.

Answer

f3lix picture f3lix · Mar 13, 2009

You take the date 1970-01-01T00:00:00 and add as many milliseconds as the value of the stamp tells you:

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.w3.org/TR/xhtml1/strict">

    <xsl:template match="node">
        <xsl:value-of
select='xs:dateTime("1970-01-01T00:00:00") + @stamp * xs:dayTimeDuration("PT0.001S")'/>
    </xsl:template>

</xsl:stylesheet>