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

<doc>
  <longdate>Jan-14-2006 08:55:22</longdate>
</doc>

XSLT

<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" />

   

    <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) < 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

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.