Tuesday, February 7, 2012

Find Remaining of Days in XSL

Scenario:
I have a List for keeping track of my Outsourced resources with fields: Resource Name, Skills, Contract Start Date, End Date, etc. What I want is: Display the No.of remaining days in a page. So Added a calculated field , applied the formula : =IF(Today>=[End Date],0,INT([To Date]-Today)), So simple, isn't it?

hmm, The problem here is: since its a calculated field, it wont get update until you update the record! All I wanted is just to display the remaining days.

Tried the formula:

<xsl:variable name="DaysDiff" select="ddwrt:FormatDateTime(string(ddwrt:Today()), 1033, 'yyyyMMdd') - ddwrt:FormatDateTime(string(@EndDate), 1033, 'yyyyMMdd')"/>

Nope! didn't work, gave me wrong values in some cases.

After Googling some time, found in Technet forums something works: Custom XSL Template!
Insert this code in Dataview XSL
<xsl:template name="DateDiff">
    <xsl:param name="StartDate"></xsl:param>
    <xsl:param name="TodayDate"></xsl:param>
 
    <xsl:variable name="JulianToday">
      <xsl:call-template name="calculate-julian-day">
        <xsl:with-param name="Year" select="substring(ddwrt:FormatDateTime(string($TodayDate), 1033, 'yyyyMMdd'),0,5)"/>
 
        <xsl:with-param name="Month" select="substring(ddwrt:FormatDateTime(string($TodayDate), 1033, 'yyyyMMdd'),5,2)"/>
 
        <xsl:with-param name="Day" select="substring(ddwrt:FormatDateTime(string($TodayDate), 1033, 'yyyyMMdd'),7,2)"/>
    </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="JulianStartDate">
      <xsl:call-template name="calculate-julian-day">
        <xsl:with-param name="Year" select="substring(ddwrt:FormatDateTime(string($StartDate), 1033, 'yyyyMMdd'),0,5)"/>
 
        <xsl:with-param name="Month" select="substring(ddwrt:FormatDateTime(string($StartDate), 1033, 'yyyyMMdd'),5,2)"/>
 
        <xsl:with-param name="Day" select="substring(ddwrt:FormatDateTime(string($StartDate), 1033, 'yyyyMMdd'),7,2)"/>
    </xsl:call-template>
    </xsl:variable>
 
    <xsl:value-of select="$JulianStartDate - $JulianToday"></xsl:value-of>
  </xsl:template>
 
  <xsl:template name="calculate-julian-day">
    <xsl:param name="Year"/>    
    <xsl:param name="Month"/>    
    <xsl:param name="Day"/>    
 
    <xsl:variable name="JulianDay" select="floor((14 - $Month) div 12)"/>    
    <xsl:variable name="JulianYear" select="$Year + 4800 - $JulianDay"/>    
    <xsl:variable name="JulianMonth" select="$Month + 12 * $JulianDay - 3"/>    
 
   
 <xsl:value-of select="$Day + floor((153 * $JulianMonth + 2) div 5) +
 $JulianYear * 365 + floor($JulianYear div 4) - floor($JulianYear div 
100) + floor($JulianYear div 400) - 32045"/>
  </xsl:template>

Call the Template with appropriate parameters:
<!-- get the delta in days between the due date and today -->
      <xsl:variable name="dueDateDelta">
         <xsl:call-template name="DateDiff">
             <xsl:with-param name="StartDate" select="substring-before(@To_x0020_Date,'T')"/>
             <xsl:with-param name="TodayDate" select="substring-before(ddwrt:TodayIso(),'T')"/>
           </xsl:call-template>
        </xsl:variable>
        
  <xsl:choose>
          <xsl:when test="$dueDateDelta &gt; 0">
    <xsl:value-of select="$dueDateDelta" />
          </xsl:when>
          <xsl:otherwise>
             <xsl:value-of select="0" />
         </xsl:otherwise>
  </xsl:choose>        


Thanks to: http://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/aeda905b-9bc6-40c4-bd22-21306c5cb0d2/

Found another Trick using javascript: http://blog.pathtosharepoint.com/2008/08/25/a-countdown-for-tasks-lists/
http://blog.metrostarsystems.com/2009/11/13/performing-datetime-calculation-in-dvwp-using-xslt/
http://sympmarc.com/2009/07/22/date-arithmetic-in-sharepoint-dvwps/



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

1 comment :

  1. In case you have any function to add days to a date pls, let me know, thx!

    ReplyDelete

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...