Dedra L. Welch
 Software Engineer | IT Architect | Web Developer
Menu
Dedra L. Welch Software Engineer | IT Architect | Web Developer
Menu

XSLT and XPath 1.0 Date Arithmetic Templates

2012-11-16

XPATH 1.0 does not provide date arithmetic functions.1 So I created these XSL templates to produce the same or more precise results than the VBScript date functions. The DateAdd and DateDiff templates use seconds since Epoch (January 1, 1970). A year is defined to be a leap year if (year mod 400 = 0) or [ (year mod 100 != 0) and (year mod 4 = 0) ]. Try it out by modfying the highlighted fields and clicking the "Transform It" button.

Original XML

<dateadd>
 <startdate> </startdate>
 <numtoadd> </numtoadd>
 <dateinterval> </dateinterval>
</dateadd>
<datediff>
 <datea> </datea>
 <dateb> </dateb>
 <dateinterval> </dateinterval>
</datediff>

Results

Template
2012-10-21 + -120 days = 06/23/2012
Difference between 2012-10-21 and 2011-09-03 is
-414 days
 
VBScript
2012-10-21 + -120 days = 6/23/2012
Difference between 2012-10-21 and 2011-09-03 is
-414 days



1. Recently, Marc D. Anderson posted an article about an undocumented ddwrt function - DateTimeTick - that you can use to find the difference between dates: Calculate Days between Two SharePoint List Dates in XSL Using ddwrt:DateTimeTick
    Close