How to get only business days between two dates in xslt 1.0

爱⌒轻易说出口 提交于 2019-12-08 02:56:37

问题


I need help to count only business days (i.e excluding Saturday and Sunday ) between two dates in xslt 1.0


回答1:


count only business days (i.e excluding Saturday and Sunday ) between two dates in xslt 1.0

If it can be assumed that the two given dates will not fall on Saturday or Sunday, you could use the method shown in the following example:

XSLT 1.0

<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:template match="/">
    <output>
        <workdays>
            <xsl:call-template name="duration-in-workdays">
                <xsl:with-param name="start-date" select="'2015-04-02'" />
                <xsl:with-param name="end-date" select="'2015-04-08'" />
            </xsl:call-template>
        </workdays>
    </output>
</xsl:template> 

<xsl:template name="duration-in-workdays">
    <!-- assumes start-date and end-date are both workdays -->
    <xsl:param name="start-date"/>
    <xsl:param name="end-date"/>
    <xsl:variable name="start">
        <xsl:call-template name="JDN">
            <xsl:with-param name="date" select="$start-date" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="end">
        <xsl:call-template name="JDN">
            <xsl:with-param name="date" select="$end-date" />
        </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="start-weekday" select="($start + 1) mod 7" />
    <xsl:variable name="end-weekday" select="($end + 1) mod 7" />
    <xsl:variable name="weeks" select="floor(($end - $start) div 7)" />
    <xsl:variable name="days" select="($end - $start) mod 7" />

    <xsl:value-of select="5 * $weeks + $days - 2*($start-weekday > $end-weekday)"/>
</xsl:template> 

<xsl:template name="JDN">
    <xsl:param name="date"/>
    <xsl:variable name="year" select="substring($date, 1, 4)"/>
    <xsl:variable name="month" select="substring($date, 6, 2)"/>
    <xsl:variable name="day" select="substring($date, 9, 2)"/>
    <xsl:variable name="a" select="floor((14 - $month) div 12)"/>
    <xsl:variable name="y" select="$year + 4800 - $a"/>
    <xsl:variable 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:stylesheet>

</xsl:stylesheet>

Caveat: not tested thoroughly.



来源:https://stackoverflow.com/questions/29411708/how-to-get-only-business-days-between-two-dates-in-xslt-1-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!