How to Format Datetime in Xslt?
Is how to Format Datetime in Xslt worth your attention? Read the pros and cons in our dedicated article.
This is my input xml file
<Root>
<child>
<element_DateCreated> 2022-05-25T21:34:36Z </element_DateCreated>
</child
</Root>
Input datetime format : 2022-05-25T21:34:36Z
Expected datetime format : 20220525213436
How to convert datetime in below xslt?
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl=""
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<data xmlns:xsi="" xsi:noNamespaceSchemaLocation="../lib/extractpad.xsd" >
<dataheaders>
<xsl:for-each>
<dataheader>
<dstamp>
<xsl:value-of select="//Root/child/element_DateCreated"/>
</dstamp>
</dataheader>
</xsl:for-each>
</dataheaders>
</data>
</xsl:template>
</xsl:stylesheet>
1 Answer
In XSLT 2.0 or higher you can use:
<xsl:value-of select="format-dateTime(/Root/child/element_DateCreated, '[Y0001][M01][D01][H01][m01][s01]')"/>
In any version you can do:
<xsl:value-of select="translate(/Root/child/element_DateCreated, '-:TZ ', '')"/>