Search
Close this search box.

XSLT – format date

I had someone ask today how to format a date in xslt 1.0.  I thought it was worth posting the answer I gave.  I hope you find it useful.  It’s amazing how many lines of code it takes for this, but it’s surprising fast. 

convert
a date represented as “Jan-14-2006 08:55:22 (CST)” to “2006-01-14T08:55:22”.

XML Sample

<?xml version="1.0" encoding="utf-8"?>
<doc>
  <longdate>Jan-14-2006 08:55:22</longdate>
</doc>

XSLT

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">

    <doc>

      <xsl:element name="newdate">

        <xsl:call-template name="FormatDate">

          <xsl:with-param name="DateTime" select="doc/longdate"/>

        </xsl:call-template>

      </xsl:element>

    </doc>

  </xsl:template>

 

  <xsl:template name="FormatDate">

    <xsl:param name="DateTime" />

    <!-- new date format 2006-01-14T08:55:22 -->

    <xsl:variable name="mo">

      <xsl:value-of select="substring($DateTime,1,3)" />

    </xsl:variable>

    <xsl:variable name="day-temp">

      <xsl:value-of select="substring-after($DateTime,'-')" />

    </xsl:variable>

    <xsl:variable name="day">

      <xsl:value-of select="substring-before($day-temp,'-')" />

    </xsl:variable>

    <xsl:variable name="year-temp">

      <xsl:value-of select="substring-after($day-temp,'-')" />

    </xsl:variable>

    <xsl:variable name="year">

      <xsl:value-of select="substring($year-temp,1,4)" />

    </xsl:variable>

    <xsl:variable name="time">

      <xsl:value-of select="substring-after($year-temp,' ')" />

    </xsl:variable>

    <xsl:variable name="hh">

      <xsl:value-of select="substring($time,1,2)" />

    </xsl:variable>

    <xsl:variable name="mm">

      <xsl:value-of select="substring($time,4,2)" />

    </xsl:variable>

    <xsl:variable name="ss">

      <xsl:value-of select="substring($time,7,2)" />

    </xsl:variable>

    <xsl:value-of select="$year"/>

    <xsl:value-of select="'-'"/>

    <xsl:choose>

      <xsl:when test="$mo = 'Jan'">01</xsl:when>

      <xsl:when test="$mo = 'Feb'">02</xsl:when>

      <xsl:when test="$mo = 'Mar'">03</xsl:when>

      <xsl:when test="$mo = 'Apr'">04</xsl:when>

      <xsl:when test="$mo = 'May'">05</xsl:when>

      <xsl:when test="$mo = 'Jun'">06</xsl:when>

      <xsl:when test="$mo = 'Jul'">07</xsl:when>

      <xsl:when test="$mo = 'Aug'">08</xsl:when>

      <xsl:when test="$mo = 'Sep'">09</xsl:when>

      <xsl:when test="$mo = 'Oct'">10</xsl:when>

      <xsl:when test="$mo = 'Nov'">11</xsl:when>

      <xsl:when test="$mo = 'Dec'">12</xsl:when>

    </xsl:choose>

    <xsl:value-of select="'-'"/>

    <xsl:if test="(string-length($day) &lt; 2)">

      <xsl:value-of select="0"/>

    </xsl:if>

    <xsl:value-of select="$day"/>

    <xsl:value-of select="'T'"/>

    <xsl:value-of select="$hh"/>

    <xsl:value-of select="':'"/>

    <xsl:value-of select="$mm"/>

    <xsl:value-of select="':'"/>

    <xsl:value-of select="$ss"/>

  </xsl:template>

</xsl:stylesheet>

Cheers,

John Workman

This article is part of the GWB Archives. Original Author: John Workman

Related Posts