30Sep/090
Get a previous date in pure xslt
Sometimes you can't use any fancy XSLT extensions and you got to go back to the basics. That's what happened to me today when I needed to find out what date it was ($variableDate - x days) ago. I found a couple of different solutions out there but most of the times they where dependent on some kind of extension and it wouldn't work in my environment.
Instead I created my own set of functions to accomplish this. The function will only work if you need to find out a past date that does not differ more than two months.
For example, (2009-9-20 - 30 days) will work, but not 2009-9-20 - 60 days)
I only needed to find out that day it was 3 days from date X so this solution sufficed.
See the XSLT code here below:
<!-- Returns the days of a specific months depending on year and date. Takes leap year into consitheration --> <xsl:template name="daysOfMonth"> <xsl:param name="month"/> <xsl:param name="year"/> <xsl:variable name="m" select="number($month)"/> <xsl:variable name="y" select="number($year)"/> <xsl:variable name="leapyear" select="($y mod 4 = 0 and $y mod 100 != 0) or $y mod 400 = 0"/> <xsl:choose> <xsl:when test="$m = 1">31</xsl:when> <xsl:when test="$m = 2"><xsl:choose><xsl:when test="$leapyear = false()">28</xsl:when><xsl:otherwise>29</xsl:otherwise></xsl:choose></xsl:when> <xsl:when test="$m = 3">31</xsl:when> <xsl:when test="$m = 4">30</xsl:when> <xsl:when test="$m = 5">31</xsl:when> <xsl:when test="$m = 6">30</xsl:when> <xsl:when test="$m = 7">31</xsl:when> <xsl:when test="$m = 8">31</xsl:when> <xsl:when test="$m = 9">30</xsl:when> <xsl:when test="$m = 10">31</xsl:when> <xsl:when test="$m = 11">30</xsl:when> <xsl:when test="$m = 12">31</xsl:when> </xsl:choose> </xsl:template> <!-- Formats a date ex. 2009-9-9 to 2009-09-09 so it can be evaluated --> <xsl:template name="returnEvalDate"> <xsl:param name="date"/> <xsl:variable name="year" select="number(substring($date,1,4))"/> <xsl:variable name="month" select="number(substring-before(substring($date,6),'-'))"/> <xsl:variable name="day" select="number(substring-after(substring($date,string-length($date)-2),'-'))"/> <xsl:value-of select="$year"/>-<xsl:if test="string-length(normalize-space($month)) = 1">0</xsl:if><xsl:value-of select="$month"/>-<xsl:if test="string-length(normalize-space($day)) = 1">0</xsl:if><xsl:value-of select="$day"/> </xsl:template> <xsl:template name="getPastDate"> <xsl:param name="now"/> <xsl:param name="daysfromnow"/> <xsl:variable name="nowY" select="number(substring($now,1,4))"/> <xsl:variable name="nowM" select="number(substring-before(substring($now,6),'-'))"/> <xsl:variable name="nowD" select="number(substring-after(substring($now,string-length($now)-2),'-'))"/> <xsl:variable name="requestedDate"> <xsl:choose> <xsl:when test="number($nowD - $daysfromnow) < 1"> <xsl:variable name="month"><xsl:choose><xsl:when test="number($nowM - 1) > 0"><xsl:value-of select="number($nowM - 1)"/></xsl:when><xsl:otherwise>12</xsl:otherwise></xsl:choose></xsl:variable> <xsl:variable name="year"><xsl:choose><xsl:when test="number($nowM - 1) > 0"><xsl:value-of select="$nowY"/></xsl:when><xsl:otherwise><xsl:value-of select="number($nowY - 1)"/></xsl:otherwise></xsl:choose></xsl:variable> <xsl:variable name="daysOfMonth"><xsl:call-template name="daysOfMonth"><xsl:with-param name="month" select="$month"/><xsl:with-param name="year" select="$year"/></xsl:call-template></xsl:variable> <xsl:variable name="day" select="number($daysOfMonth + number($nowD - $daysfromnow))"/> <xsl:value-of select="$year"/>-<xsl:value-of select="$month"/>-<xsl:value-of select="$day"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$nowY"/>-<xsl:value-of select="$nowM"/>-<xsl:value-of select="number($nowD - $daysfromnow)"/> </xsl:otherwise> </xsl:choose> </xsl:variable> <xsl:call-template name="returnEvalDate"><xsl:with-param name="date" select="$requestedDate"/></xsl:call-template> </xsl:template>