Find Remaining Number of Days using XSL in SharePoint

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: https://social.msdn.microsoft.com/Forums/en-US/sharepointcustomization/thread/aeda905b-9bc6-40c4-bd22-21306c5cb0d2/

Found another trick using JavaScript: https://sympmarc.com/2009/07/22/date-arithmetic-in-sharepoint-dvwps/

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. Passionate about sharing the deep technical knowledge and experience to help others, through the real-world articles!

One thought on “Find Remaining Number of Days using XSL in SharePoint

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *