tags:

views:

126

answers:

5

Hi All, want to know How to convert 2009-09-18 to 18th Sept in xslt? Thanks.

A: 

what language is your backend in?

If it's PHP it's easy to format DATETIME strings with the date() function: http://uk.php.net/manual/en/function.date.php

You can format the date before passing it off into XML form.

Evernoob
yeah, thats true and the easy/best solution.but what if I have to handle it through xslt
Wondering
A: 

Using named template with a big <xsl:choose> sounds like an appropriate solution here. The tricky part is month name, the rest is pretty trivial, right? Of course you don't have to separate it to the named template.

Michael Krelin - hacker
+1  A: 

EXSLT has some extension functions for converting date strings, see: http://exslt.org/date/index.html

With a little luck your XSLT processor supports these natively, otherwise most of the date functions have a plain XSLT 1.0 implementation you can include

jor
+4  A: 

Here's a pure XSLT 1.0 solution which assumes valid input:

<xsl:template match="/">
 <newdate>
  <xsl:call-template name="convertdate">
   <xsl:with-param name="date" select="date"/>
  </xsl:call-template>
 </newdate>
</xsl:template>

<xsl:template name="convertdate"> 
 <xsl:param name="date"/>

 <xsl:variable name="day">
  <xsl:value-of select="number(substring-after(substring-after($date,'-'), '-'))"/>
 </xsl:variable>

 <xsl:variable name="suffix">
  <xsl:choose>
                        <xsl:when test="substring($day, string-length($day), 1) = '1' and not(starts-with($day, '1'))">st</xsl:when>
   <xsl:when test="substring($day, string-length($day), 1) = '2' and not(starts-with($day, '1'))">nd</xsl:when>
   <xsl:when test="substring($day, string-length($day), 1) = '3' and not(starts-with($day, '1'))">rd</xsl:when>
   <xsl:otherwise>th</xsl:otherwise>
       </xsl:choose>
 </xsl:variable>

 <xsl:variable name="mo">
  <xsl:value-of select="number(substring-before(substring-after($date,'-'), '-'))"/>
 </xsl:variable>  

 <xsl:variable name="month">
  <xsl:choose>
   <xsl:when test="$mo = 1">Jan</xsl:when>
   <xsl:when test="$mo = 2">Feb</xsl:when>
   <xsl:when test="$mo = 3">Mar</xsl:when>
   <xsl:when test="$mo = 4">Apr</xsl:when>
   <xsl:when test="$mo = 5">May</xsl:when>
   <xsl:when test="$mo = 6">Jun</xsl:when>
   <xsl:when test="$mo = 7">Jul</xsl:when>
   <xsl:when test="$mo = 8">Aug</xsl:when>
   <xsl:when test="$mo = 9">Sept</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:variable>

 <xsl:value-of select="$day"/><xsl:value-of select="$suffix"/>&#160;<xsl:value-of select="$month"/>

</xsl:template>
Andy
Nice, +1. :) And using non-breaking spaces is actually also a good idea.
Tomalak
Thanks, will try this one,
Wondering
A: 

Along the lines of:

<xsl:template name="friendly-date">
  <xsl:param name="datestring" select="'yyyy-mm-dd'" />

  <xsl:variable name="y" select="number(substring-before($datestring, '-'))" />
  <xsl:variable name="r" select="number(substring-after($datestring, '-'))" />
  <xsl:variable name="m" select="number(substring-before($r, '-'))" />
  <xsl:variable name="d" select="number(substring-after($r, '-'))" />

  <xsl:choose>
    <xsl:when test="
      $y &gt;= 1970 and $y &lt;= 9999
      and
      $m &gt;= 1 and $m &lt;= 12
      and
      $d &gt;= 1 and $m &lt;= 31
    ">
      <xsl:value-of select="$d" />
      <xsl:text> </xsl:text>
      <xsl:choose>
        <xsl:when test="$m = 1">
          <xsl:value-of select="'Jan'" />
        </xsl:when>
        <!-- ... insert missing ... -->
        <xsl:when test="$m = 12">
          <xsl:value-of select="'Dec'" />
        </xsl:when>
      </xsl:choose>
      <xsl:text> </xsl:text>
      <xsl:value-of select="$y" />
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$datestring" />
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>

Beware that this will not check for impossible dates. It depends on your input if that can become a problem or not.

Apart from that, it does not do international date formats. For less ad-hoc solutions, I recommend a look at EXSLT, like the other do.

Tomalak