Question

I need to know all weekends from two dates. I actually got a template which returns the day number (0-6) when passing a date as parameter, however, the problem is i need to loop through the dates between the two dates and calculate just business days.

Is there a way to store all these dates in an array and loop through that array? Do i need to use C# embedded?

I'm using XSLT V 1.0 with SharePoint Designer.

Thanks.

Was it helpful?

Solution

This is not exactly trivial to do in pure XSLT 1.0.

Weekend dates in a given range:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:param name="startDate" select="'2014-05-01'"/>
<xsl:param name="endDate" select="'2014-05-31'"/>

<xsl:template match="/">
    <weekends>
        <xsl:call-template name="weekends">
            <xsl:with-param name="startJDN">
                <xsl:call-template name="JDN">
                    <xsl:with-param name="date" select="$startDate" />
                </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="endJDN">
                <xsl:call-template name="JDN">
                    <xsl:with-param name="date" select="$endDate" />
                </xsl:call-template>
            </xsl:with-param>
        </xsl:call-template>
    </weekends>
</xsl:template> 

<xsl:template name="weekends">
    <xsl:param name="startJDN"/>
    <xsl:param name="endJDN"/>
    <xsl:if test="$startJDN mod 7 > 4">
        <date>
            <xsl:call-template name="GD">
                <xsl:with-param name="JDN" select="$startJDN" />
            </xsl:call-template>
        </date>
    </xsl:if>
    <xsl:if test="$startJDN &lt; $endJDN">
        <xsl:call-template name="weekends">
            <xsl:with-param name="startJDN" select="$startJDN + 1"/>
            <xsl:with-param name="endJDN" select="$endJDN"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template> 

<xsl:template name="JDN">
    <xsl:param name="date"/>
    <xsl:param name="year" select="substring($date, 1, 4)"/>
    <xsl:param name="month" select="substring($date, 6, 2)"/>
    <xsl:param name="day" select="substring($date, 9, 2)"/>
    <xsl:param name="a" select="floor((14 - $month) div 12)"/>
    <xsl:param name="y" select="$year + 4800 - $a"/>
    <xsl:param name="m" select="$month + 12*$a - 3"/>
    <xsl:value-of select="$day + floor((153*$m + 2) div 5) + 365*$y + floor($y div 4) - floor($y div 100) + floor($y div 400) - 32045" />
</xsl:template> 

<xsl:template name="GD">
    <xsl:param name="JDN"/>
    <xsl:param name="f" select="$JDN + 1401 + floor((floor((4 * $JDN + 274277) div 146097) * 3) div 4) - 38"/>
    <xsl:param name="e" select="4*$f + 3"/>
    <xsl:param name="g" select="floor(($e mod 1461) div 4)"/>
    <xsl:param name="h" select="5*$g + 2"/>
    <xsl:param name="D" select="floor(($h mod 153) div 5 ) + 1"/>
    <xsl:param name="M" select="(floor($h div 153) + 2) mod 12 + 1"/>
    <xsl:param name="Y" select="floor($e div 1461) - 4716 + floor((14 - $M) div 12)"/>
    <xsl:value-of select="concat($Y, '-', format-number($M, '00'), '-', format-number($D, '00'))" />
</xsl:template>     

</xsl:stylesheet>

The result of the above is a list of all Saturdays and Sundays in the month of May 2014:

<?xml version="1.0" encoding="UTF-8"?>
<weekends>
  <date>2014-05-03</date>
  <date>2014-05-04</date>
  <date>2014-05-10</date>
  <date>2014-05-11</date>
  <date>2014-05-17</date>
  <date>2014-05-18</date>
  <date>2014-05-24</date>
  <date>2014-05-25</date>
  <date>2014-05-31</date>
</weekends>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top