datetimexsltxslt-2.0

Display all dates between two different dates using XSLT


I need a correct logic that works on XSLT 2.0 that display all dates between given two different dates. For example if

I need display results like: 2022-10-31, 2022-11-30, 2022-12-31, 2023-01-31 , 2023-02-28, 2023-03-31, 2023-04-30

I figure out the date part, but I don't know how to increment month and year part exactly. When month reaches 12 (dec), it again should start from 1 (jan) and simultaneously increment current year count. What can I try next?


Solution

  • Calculate the number of days between the dates: fn:days-from-duration((xs:date('2023-04-01') - xs:date('2022-10-01'))

    and then produce a sequence of 1 to the number of days in the duration, convert that number into an xs:dayTimeDuration() of days, and add those days to the start date:

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions"
        exclude-result-prefixes="xs"
        version="2.0">
        <xsl:template match="/">
            <xsl:variable name="start" select="xs:date('2023-04-01')"/>
            <xsl:variable name="end" select="xs:date('2022-10-01')"/>
            <xsl:sequence select="(1 to fn:days-from-duration(($start - $end )) ) ! ($start + xs:dayTimeDuration('P'||.||'D') )"/>
        </xsl:template>
    </xsl:stylesheet>