Date Calculations - InfoPath Dev
in

InfoPath Dev

Date Calculations

Last post 02-03-2010 06:45 AM by Jimmy. 11 replies.
Page 1 of 1 (12 items)
Sort Posts: Previous Next
  • 10-25-2005 01:35 AM

    • 199840
    • Not Ranked
    • Joined on 10-25-2005
    • Posts 9

    Date Calculations

    I hope this is simple, two date pickers i just want to find the difference between the two date out putted in a text field.

    Please help
  • 10-25-2005 12:08 PM In reply to

    Hi 199840 and welcome to our forum,
    Do you want the units in years, months, days, hours, minutes, seconds, or milliseconds? You'll likely have to write some code to subtract each of the major units and convert. Should be pretty simple though. Have you searched for other threads on date calculations?

    Patrick Halstead [InfoPath MVP]
    InfoPathDev
    Patrick Halstead
    Project Manager at Qdabra
  • 10-25-2005 09:58 PM In reply to

    Hi,

    Here is a good reference for this:
    http://blogs.msdn.com/infopath/archive/2005/01/25/360318.aspx

    Regards,

    Franck Dauché
  • 10-25-2005 11:24 PM In reply to

    Hi, just another question about it: does the date management change in relation to the regional setting of the machine? For example when you select a part of a date by code?
  • 10-26-2005 03:29 AM In reply to

    • 199840
    • Not Ranked
    • Joined on 10-25-2005
    • Posts 9
    I have seen that link, however it was a litte complicated, im struggling to know which language to write in, when using managed code in InfoPath, my default setting appears to be Jscript.
    I only really need to know the difference between the two dates in days. eg, difference between 1st nov 2005 and 2nd Nov 2005 is 1 day.
    Any ideas????
  • 10-28-2005 10:36 PM In reply to

    Tatopitta: the code will work regardless of the regional setting.
    199840: if you are more familiar with JScript, I recommend you use that.


    Patrick Halstead [InfoPath MVP]
    InfoPathDev
    Patrick Halstead
    Project Manager at Qdabra
  • 10-31-2005 07:29 AM In reply to

    Working with date calculations in most programming languages is usually a pain... Maybe we should change the world's calendar to better suit our needs.

    ---------------
    Matt Faus
    Matt Faus / Microsoft InfoPath MVP
    Qdabra® Software / Streamline data gathering to turn process into knowledge
  • 01-11-2008 08:46 PM In reply to

    I know this is an old post, but I have seen quite some questions on date calculations and posts to the msdn site, which I looked through and really didn't help me much.  So I came up with this, hopefully new people will find it helpful.  Simply takes the  difference between two date picker boxes in infopath(2003 or 2007).  Also it was about time I posted some useful information to this great site.

    My two date fields were: StartDate and EndDate, and the difference was saved in DaysOff text box. 

    I also used an on after event for end date.

    VB.net Code (backward compatible with InfoPath 2003):

    Imports Microsoft.VisualBasic.DateAndTime (reference that at the top of the code under: Imports Microsoft.Office.Interop.InfoPath.SemiTrust)

    'If StartDate and EndDate is not blank, and End Date is greater than StartDate then calculate difference.

    If thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:StartDate").text <> "" And _

    thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:EndDate").text <> "" And _

    System.Convert.ToString(thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:EndDate").text) > _

    System.Convert.ToString(thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:StartDate").text) Then

    'Calculates the difference between StartDate and EndDate.

    Dim datStart As Date = thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:StartDate").text Dim datEnd As Date = thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:AbsenceDetails/my:EndDate").text

    'Add one day to correct the calculation.

    Dim strDaysOff As String = (DateDiff(Microsoft.VisualBasic.DateInterval.Day, datStart, datEnd) + 1)

    'Assigns the result to DaysOff

    thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:DaysOff").text = strDaysOff

    Else

    thisXDocument.DOM.selectSingleNode("/dfs:myFields/my:DaysOff").text = ""

    End If

     I know the code is rudamentary, and probably violated some code, or the code could be trimmed down, but it works, and hopefully others will find it usefull.

    Also should be noted the +1 was added to the date difference because in my case I needed to know the number of days between the two dates(start and end date).  For example For a work week that is monday through friday, using the code above would net a result of 5, because the time span is 5 days, however if you don't put +1 on there then it would be 4 as friday is 4 days after monday.  I also would put a rule in infopath to pop a message up if the end date is before the first date, and then zero the box making the user reselect a date later than the start date.

  • 02-02-2010 01:22 PM In reply to

    Why is it that MS never seems to include their standard database functions in new products like info path? DateDiff is a a function that has been around since the earliest launches of MS Access. This should be a standard item listed in the functions for a field in Info Path. There are many other examples of functions they have left out of infopath, forcing you to go and start learning .NET code when you may just be a power user who certainly can develop forms until MS forces you to code. Ridiculous....

  • 02-02-2010 04:08 PM In reply to

    It's possible to get a date difference without using code:

    [originally posted link deleted]

    http://alecpojidaev.wordpress.com/2008/12/30/infopath-codeless-programming-walkthrough-2/

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 02-03-2010 05:46 AM In reply to

  • 02-03-2010 06:45 AM In reply to

     Sorry Alec, I didn't realize that.  I've edited my post to use your link instead of the other one.  Feel free to edit your post as well.

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (12 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.