XDocument.DOM.setProperty "SelectionNamespaces", "xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2009-05-01T18:01:38""" Function ISODateStringToVBDate(ISODateString) Dim dtRetVal dtRetVal = Null If Trim(ISODateString) <> "" Then dtRetVal = DateSerial(Mid(ISODateString, 1, 4), _ Mid(ISODateString, 6, 2), Mid(ISODateString, 9, 2)) End If ISODateStringToVBDate = dtRetVal End Function Sub CalculateDateDifference() Dim strStartDate Dim strEndDate Dim strDateInterval Dim dtStart Dim dtEnd Dim intDateDiff Dim blnInWorkdays Dim intCurDayNo Dim dtCurDate Dim intWorkdaysCounter 'Retrieve the ISO date from the InfoPath date fields strStartDate = XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:startDate").text strEndDate = XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:endDate").text 'Convert ISO date into VBScript date dtStart = ISODateStringToVBDate(strStartDate) dtEnd = ISODateStringToVBDate(strEndDate) If Not IsNull(dtStart) And Not IsNull(dtEnd) Then 'Retrieve the date interval to be used strDateInterval = XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:dateInterval").text Select Case LCase(strDateInterval) Case "yyyy" 'Difference between dates in years intDateDiff = DateDiff("yyyy", dtStart, dtEnd) Case "m" 'Difference between dates in months intDateDiff = DateDiff("m", dtStart, dtEnd) Case "d" 'Difference between dates in days intDateDiff = DateDiff("d", dtStart, dtEnd) Case "h" 'Difference between dates in hours intDateDiff = DateDiff("h", dtStart, dtEnd) Case "n" 'Difference between dates in minutes intDateDiff = DateDiff("n", dtStart, dtEnd) Case "s" 'Difference between dates in seconds intDateDiff = DateDiff("s", dtStart, dtEnd) Case Else 'If interval is not recognized so use difference in days intDateDiff = DateDiff("d", dtStart, dtEnd) End Select 'Set the value of the dateDiff field XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:dateDiff").text = intDateDiff ' Check whether date difference should shown in work days. ' This only works for option "d" when difference in amount of ' days is being calculated If LCase(strDateInterval) = "d" Then ' Retrieve whether work days should be shown blnInWorkdays = XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:inWorkdays").text If CBool(blnInWorkdays) = True Then intWorkdaysCounter = 0 ' Loop through all the dates and count the work days For i = 0 To intDateDiff - 1 dtCurDate = DateAdd("d", i, dtStart) intCurDayNo = Weekday(dtCurDate) If intCurDayNo > 1 And intCurDayNo < 7 Then intWorkdaysCounter = intWorkdaysCounter + 1 End If Next ' Set the date difference field to display the amount ' of work days between the two dates XDocument.DOM.selectSingleNode _ ("/my:myFields/my:h/my:dateDiff").text = intWorkdaysCounter End If End If End If End Sub 'Start Date _attr_ deleted Sub msoxd_my_startDate_OnAfterChange(eventObj) If eventObj.IsUndoRedo Then Exit Sub End If CalculateDateDifference End Sub 'End Date Sub msoxd_my_endDate_OnAfterChange(eventObj) If eventObj.IsUndoRedo Then Exit Sub End If CalculateDateDifference End Sub