Calculating date and time differences in infoPath - InfoPath Dev
in

InfoPath Dev

Having trouble finding a blog or post that answers your question? Check out our Custom Search Page

Calculating date and time differences in infoPath

Last post 02-07-2012 04:58 PM by traveladic. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 11-13-2008 08:36 AM

    Calculating date and time differences in infoPath

    Hello -

    I have a browser based infopath form that I would like to have calculate the time that may or may not cross days. The article posted at:

    http://www.bizsupportonline.net/infopath2003/date-time-difference-calculations-vbscript.htm

    is exactly what I am looking for, but it is written in VBScript, InfoPath 2003, and I need a browser enabled form, InfoPath 2007. As an example, if the user enters the start date as 11/13/08 with a start time of 11:59 pm

    and an end time of 11/14/08 with an end time of 3:00 am, the time calculation should be 3  hours and 1 minute.

     

    Thanks

  • 11-13-2008 07:59 PM In reply to

    Re: Calculating date and time differences in infoPath

    Thanks,
    Sandhya


  • 11-14-2008 10:22 PM In reply to

    Re: Calculating date and time differences in infoPath

    Chandrakanth/GGKTech
  • 11-16-2008 06:21 PM In reply to

    Re: Calculating date and time differences in infoPath

    Thank you. I went through the code, and this will be a great help. But the problem I am having is that I need the time between the days calculated, not just the days. For example, the application I am working on has a "Duration of Maintenance" field to calculate possible service outages. If a user enters a start date of 11/18/08 and an end date of 11/19/08 with a start time related to the 11/18/08 date of 11:59 pm and an end time related to the end date of 11/19/08 of 3:00 am, I need the duration of maintenance field to auto fill with a duration of 3 hours and 1 minute. I can calculated the time, but not across days.

    Thanks again for your help.

     

  • 11-16-2008 08:15 PM In reply to

    Re: Calculating date and time differences in infoPath

    Code like this would do the trick.  Of course in actuality you would want to perform error checking first, to make sure that none of the input values is blank, and that endDateCombined occurs after startDateCombined. It would also make sense factor the "xsi:nil" removal code out into a separate method, since it's used three times.
    string xsiNamespace = NamespaceManager.LookupNamespace("xsi");
    XPathNavigator mdsNav = MainDataSource.CreateNavigator();
    mdsNav.MoveToChild("myFields", NamespaceManager.LookupNamespace("my"));

    DateTime startDate = mdsNav.SelectSingleNode("my:startDate", NamespaceManager).ValueAsDateTime;
    DateTime startTime = mdsNav.SelectSingleNode("my:startTime", NamespaceManager).ValueAsDateTime;

    DateTime endDate = mdsNav.SelectSingleNode("my:endDate", NamespaceManager).ValueAsDateTime;
    DateTime endTime = mdsNav.SelectSingleNode("my:endTime", NamespaceManager).ValueAsDateTime;

    DateTime startDateCombined = startDate.Add(startTime.TimeOfDay);
    DateTime endDateCombined = endDate.Add(endTime.TimeOfDay);
    TimeSpan diff = endDateCombined.Subtract(startDateCombined);

    XPathNavigator nodeNav = mdsNav.SelectSingleNode("my:dayDiff", NamespaceManager);
    if (nodeNav.MoveToAttribute("nil", xsiNamespace))
    {
         nodeNav.DeleteSelf();
    }
    nodeNav.SetValue(diff.Days.ToString());

    nodeNav = mdsNav.SelectSingleNode("my:hourDiff", NamespaceManager);
    if (nodeNav.MoveToAttribute("nil", xsiNamespace))
    {
         nodeNav.DeleteSelf();
    }
    nodeNav.SetValue(diff.Hours.ToString());

    nodeNav = mdsNav.SelectSingleNode("my:minuteDiff", NamespaceManager);
    if (nodeNav.MoveToAttribute("nil", xsiNamespace))
    {
         nodeNav.DeleteSelf();
    }
    nodeNav.SetValue(diff.Minutes.ToString());

    Jimmy Rishe / Software Developer / InfoPath MVP
    Qdabra Software
  • 02-07-2012 04:58 PM In reply to

    Re: Calculating date and time differences in infoPath

    I have a similar situation but I need to account for working hours, for example if the working hours are 7am to 5pm and job started on the 1-Jan-12 at 4pm and it was completed at 2-Jan-12 8am then the total should be 2hrs not 16hr. and as some add fun there are weekends and holidays.
Page 1 of 1 (6 items)
Copyright © 2003-2012 Qdabra Software. All rights reserved.
View our Terms of Use.