SharePoint Data View Web Parts (DVWPs) can be greatly customized using XSL transformations (XSLT). One of the great capabilities provided is the ability to render HTML conditionally using XPath expressions. While XPath can be pretty intuitive, date comparisons might not behave as expected. For example, performing a test to see if two date values equal each other may work perfectly while tests to see if one if greater than the other might not!

  • @DueDate > @DateCompleted
  • @DueDate = @DateCompleted
  • @DueDate < @DateCompleted

This is because XPath supports text and numeric comparisons. The equality comparison behaves properly because string values of the same date will be identical, whereas string values of different dates are neither greater than nor less than each other. Since the only available option is to perform a numeric comparison, it’s necessary to convert the date values into usable number values!

Microsoft’s ddwrt namespace contains some helpful Data View Web Part Extension Functions, particularly two: FormatDate and FormatDateTime. Both functions receive string values, convert them, and return string values. While FormatDate uses predefined formats (none of which are useful in this case), FormatDateTime allows for custom formats using the standard “MM dd yyyy” style of formatting string. Thus, FormatDateTime can be used to convert a date value into a usable number value like so:

  • ddwrt:FormatDateTime(@DueDate, 1033, ‘yyyyMMdd’) > ddwrt:FormatDateTime(@DateCompleted, 1033, ‘yyyyMMdd’)
  • ddwrt:FormatDateTime(@DueDate, 1033, ‘yyyyMMdd’) = ddwrt:FormatDateTime(@DateCompleted, 1033, ‘yyyyMMdd’)
  • ddwrt:FormatDateTime(@DueDate, 1033, ‘yyyyMMdd’) < ddwrt:FormatDateTime(@DateCompleted, 1033, ‘yyyyMMdd’)

There’s no need to explicitly convert to a number since the comparison operators automatically do so (if possible). Note that is it important to use the “MM” and “dd” formats rather than the “M” and “d” formats to ensure proper mathematical comparison.