Menu Home

SharePoint xslt calculate age

This is some xslt I have been fiddling with in order to calculate the age of persons in a list from their birthday (or in fact their CPR number, the danish social security number).

Unfortunately calculated columns in SharePoint doesn’t support getting today’s date, this is because their values are calculated columns are calculated at time of storage. Thus the only viable alternative to some custom code or javascript is xslt. This template will return the age in years. It requires two input, born, the date of birth, and today, today’s date. The format of the input must be MM/dd/yyyy.

<xsl:template name="AgeInYears">
<xsl:param name="bornDate" select="."></xsl:param>
<xsl:param name="today" select="."></xsl:param>
<xsl:when test="ddwrt:FormatDateTime(string($today),1033,'MMdd') &lt; ddwrt:FormatDateTime(string($bornDate),1033,'MMdd')">
<xsl:value-of select="ddwrt:FormatDateTime(string($today),1033,'yyyy')-ddwrt:FormatDateTime(string($bornDate),1033,'yyyy')-1"></xsl:value-of>
<xsl:value-of select="ddwrt:FormatDateTime(string($today),1033,'yyyy')-ddwrt:FormatDateTime(string($bornDate),1033,'yyyy')"></xsl:value-of>

I call the template the following way:
<xsl:call-template name="AgeInYears">
<xsl:with-param name="bornDate" select="$thisNode/@*[name()=current()/@Name]"></xsl:with-param>
<xsl:with-param name="today" select="ddwrt:Today()"></xsl:with-param>

Categories: Programming

Tagged as:

Simon J.K. Pedersen

Leave a Reply

Your email address will not be published. Required fields are marked *