using xslt how do i test if a date was within the last (say) 15 days?
input:
- date in format dd/mm/yy
- X number of days
output:
- if the date occured within X days of now
eg recent(’02/07/10′,30) would return true iff 02/07/10 was 30 days in the past
some steps i got:
main func
<xsl:function name="custom:monthtodays">
<xsl:param name="date"/>
<xsl:param name="daysago"/>
<xsl:variable name="daycountnow" select="year-from-dateTime(current-dateTime())*365+day-from-dateTime(current-dateTime())+custom:monthtodays(month-from-dateTime(current-dateTime())" />
<xsl:variable name="datedaycount" select="numeric(substring($date,1,2))+numeric(substring($date,7,2))*365+custom:monthtodays(numeric(substring($date,4,2)))" />
<xsl:value-of select="$daycountnow - $datedaycount - $daysago > 0"/>
</xsl:function>
helper func
<xsl:function name="custom:monthtodays">
<xsl:param name="month"/>
<xsl:choose>
<xsl:when test="$month =1"> <xsl:value-of select="0"/> </xsl:when>
<xsl:when test="$month =2"> <xsl:value-of select="31"/> </xsl:when>
<xsl:when test="$month =3"> <xsl:value-of select="59"/> </xsl:when>
<xsl:when test="$month =4"> <xsl:value-of select="90"/> </xsl:when>
<xsl:when test="$month =5"> <xsl:value-of select="120"/> </xsl:when>
<xsl:when test="$month =6"> <xsl:value-of select="151"/> </xsl:when>
<xsl:when test="$month =7"> <xsl:value-of select="181"/> </xsl:when>
<xsl:when test="$month =8"> <xsl:value-of select="212"/> </xsl:when>
<xsl:when test="$month =9"> <xsl:value-of select="243"/> </xsl:when>
<xsl:when test="$month =10"> <xsl:value-of select="273"/> </xsl:when>
<xsl:when test="$month =11"> <xsl:value-of select="304"/> </xsl:when>
<xsl:when test="$month =12"> <xsl:value-of select="334"/> </xsl:when>
</xsl:choose>
</xsl:function>
but this doesnt take account of leap years and the like … surely there is a betterway?
Here is one way you could do it in XSLT 1.0. As you are using .Net 4.0, you should be available to use the microsoft extension function, and write your own (javascript) function to do the date difference.
Here is the transformation. Note the JavaScript function is quite crude, and assumes the date is in DD/MM/YY format:
When you apply it on this input (assuming today is 23/07/2010)
You should get the return value of true
When you apply it on this input
You should get the return value of false