Friday, May 01, 2009

Date Formatting In XSLt

I wanted a date to be changed, essentially a string containing a date time. See my previous post for some good XSLt links, but here's my solution:
<xsl:call-template name="FormatDate">
<xsl:with-param name="DateTimeParam" select="@InvoiceDate"/>
</xsl:call-template>

<xsl:template name="FormatDate">
<!-- expecting datetime example: 2009-04-20T00:00:00 -->
<xsl:param name="DateTimeParam" />


<!-- reformat date param to be easier to use -->
<xsl:variable name="DateTime">
<xsl:value-of select="substring($DateTimeParam,1,10)"/>
</xsl:variable>

<!-- new date format January 20, 2007 -->
<xsl:variable name="year">
<xsl:value-of select="substring-before($DateTime,'-')" />
</xsl:variable>
<xsl:variable name="mo-temp">
<xsl:value-of select="substring-after($DateTime,'-')" />
</xsl:variable>
<xsl:variable name="mo">
<xsl:value-of select="substring-before($mo-temp,'-')" />
</xsl:variable>
<xsl:variable name="day">
<xsl:value-of select="substring-after($mo-temp,'-')" />
</xsl:variable>

<xsl:if test="(string-length($day) &lt; 2)">
<xsl:value-of select="0"/>
</xsl:if>
<xsl:value-of select="$day"/>
<xsl:value-of select="' '"/>
<xsl:choose>
<xsl:when test="$mo = '1' or $mo = '01'">Jan</xsl:when>
<xsl:when test="$mo = '2' or $mo = '02'">Feb</xsl:when>
<xsl:when test="$mo = '3' or $mo = '03'">Mar</xsl:when>
<xsl:when test="$mo = '4' or $mo = '04'">Apr</xsl:when>
<xsl:when test="$mo = '5' or $mo = '05'">May</xsl:when>
<xsl:when test="$mo = '6' or $mo = '06'">Jun</xsl:when>
<xsl:when test="$mo = '7' or $mo = '07'">Jul</xsl:when>
<xsl:when test="$mo = '8' or $mo = '08'">Aug</xsl:when>
<xsl:when test="$mo = '9' or $mo = '09'">Sep</xsl:when>
<xsl:when test="$mo = '10'">Oct</xsl:when>
<xsl:when test="$mo = '11'">Nov</xsl:when>
<xsl:when test="$mo = '12'">Dec</xsl:when>
</xsl:choose>
<xsl:value-of select="' '"/>
<xsl:value-of select="$year"/>
</xsl:template>

No comments:

Post a Comment