How do I compare the year of a given date to int?
<if test="year(@varDate)>=1900">
true
</if>
Where the format of the date in the input file is:
<date varDate="31/12/1999" />
How do I compare the year of a given date to int?
<if test="year(@varDate)>=1900">
true
</if>
Where the format of the date in the input file is:
<date varDate="31/12/1999" />
If you are stuck with XSLT 1.0, the most common way is to extract the year-part, like so:
substring-before('1994-12-31', '-')
Or, if your format is, let's say dd/MM/yyyy
, you can do1:
substring-after('31/12/1999', '/')
In your case that would be something along those lines:
<xsl:if test="substring-after('31/12/1999', '/') > 1900">
true
</xsl:if>
Since you didn't provide input data, I assume you can work out the details yourself. Your original question was tagged xslt-1.0, but this has since changed. If that means that you can use XSLT 2.0 or higher, and you have an actual typed xs:date
value, you can also use fn:year-from-date
:
<xsl:if test="year-from-date(xs:date('1994-12-31')) > 1900">
true
</xsl:if>
1 This is not enough as there are two slashes, use substring-after(substring-after('31/12/1999', '/'), '/')
, as seen in Michael's post (credit to him).
it turns out my date is in a different format that in solution above (dd/mm/yyyy).
If that's really your date format - i.e.both days and month are always padded to two digits - you can use:
<if test="substring(@varDate, 7) >= 1900">
as your test.
If not, you'll need:
<if test="substring-after(substring-after(@varDate, '/'), '/') >= 1900">