Plain english duration between two dates in XSL

Posted: 31 March 2010 in Uncategorized

Yesterday, I published a method to format date duration calculations in plain English using javascript.

This time, I’m doing it in the XSL.

<!-- duration in days -->

 <xsl:variable name="duration"           select="number(event/scheduling/duration)" />
 <xsl:variable name="numDays"            select="format-number($duration         div (24 * 60 * 60 * 1000),'0')" />
 <xsl:variable name="leftOverDays"       select="              $duration         mod (24 * 60 * 60 * 1000)" />
 <xsl:variable name="numHours"           select="format-number($leftOverDays     div (     60 * 60 * 1000),'0')" />
 <xsl:variable name="leftOverHours"      select="              $leftOverDays     mod (     60 * 60 * 1000)" />
 <xsl:variable name="numMinutes"         select="format-number($leftOverHours    div (          60 * 1000),'0')" />
 <xsl:variable name="leftOverMinutes"    select="              $leftOverHours    mod (          60 * 1000)" />
 <xsl:variable name="numSeconds"         select="format-number($leftOverMinutes  div (               1000),'0')" />
 <xsl:variable name="numMiliSeconds"     select="              $leftOverMinutes  mod (               1000)" />

 <xsl:variable name="dayString"><xsl:choose>
 <xsl:when test="$numDays=1">1 Day</xsl:when>
 <xsl:when test="$numDays &gt; 0"><xsl:value-of select="$numDays" /> Days </xsl:when>
 </xsl:choose></xsl:variable>
 <xsl:variable name="hourString"><xsl:choose>
 <xsl:when test="$numHours=1">1 Hour </xsl:when>
 <xsl:when test="$numHours &gt; 0"><xsl:value-of select="$numHours" /> Hours </xsl:when>
 </xsl:choose></xsl:variable>
 <xsl:variable name="minuteString"><xsl:choose>
 <xsl:when test="$numMinutes=1">1 Minute </xsl:when>
 <xsl:when test="$numMinutes &gt; 0"><xsl:value-of select="$numMinutes" /> Minutes </xsl:when>
 </xsl:choose></xsl:variable>
 <xsl:variable name="secondString"><xsl:choose>
 <xsl:when test="$numSeconds=1">1 Second </xsl:when>
 <xsl:when test="$numSeconds &gt; 0"><xsl:value-of select="$numSeconds" /> Seconds </xsl:when>
 </xsl:choose></xsl:variable>
 <xsl:variable name="miliSecondString"><xsl:choose>
 <xsl:when test="$numMiliSeconds=1">1 MiliSecond </xsl:when>
 <xsl:when test="$numMiliSeconds &gt; 0"><xsl:value-of select="$numMiliSeconds" /> MiliSeconds </xsl:when>
 </xsl:choose></xsl:variable>

 <xsl:variable name="durationString">
 <xsl:value-of select="$dayString" />
 <xsl:value-of select="$hourString" />
 <xsl:value-of select="$minuteString" />
 <xsl:value-of select="$secondString" />
 <xsl:value-of select="$miliSecondString" />
 </xsl:variable>
 
 <xsl:value-of select="$durationString" />

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s