MS Office Forum / General MS InfoPath Questions / September 2005
days between dates
|
|
Thread rating:  |
Blackduke - 30 Aug 2005 10:47 GMT I have two date pickers can i populate another box with the amount of days selected
thanks
Scott L. Heim [MSFT] - 30 Aug 2005 12:48 GMT Hi,
Yes but it will require a custom code solution to do so. If you are using managed code (i.e. C#.NET or VB.NET) here is a BLOG entry that provides a sample and the sample code:
http://blogs.msdn.com/infopath/search.aspx?q=date+calculation&p=1
If you are just using script (i.e. VBScript or JScript) here is a link to numerous JScript functions for date calculations:
Date and Time Arithmetic in JScript http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvid/html/ msdn_vidateadd.asp
There have also been numerous posts in this forum regarding this and I have provided sample code in some of them. If you cannot find what you need, let me know and I will post sample steps for you.
I hope this helps!
Scott L. Heim Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights
Blackduke - 30 Aug 2005 21:26 GMT Thanks for the post, I could not find any posts relevent, if you could post some steps for me that would be great I have never looked at managed code, so if you could go easy with me!!!! that would be great
thanks for the quick responce
>Hi, > [quoted text clipped - 21 lines] > >This posting is provided "AS IS" with no warranties, and confers no rights. Scott L. Heim [MSFT] - 30 Aug 2005 21:34 GMT No problem - so would you like managed code or script? And then what language do you prefer?
Scott L. Heim Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights
Blackduke - 01 Sep 2005 21:41 GMT lol, your the expert :) could you supply the easyiest for a non devloper
thanks
>No problem - so would you like managed code or script? And then what >language do you prefer? [quoted text clipped - 3 lines] > >This posting is provided "AS IS" with no warranties, and confers no rights. Scott L. Heim [MSFT] - 01 Sep 2005 22:07 GMT I am sure I'll get "flamed" for this <G> but for date calculations, I much prefer VBScript. So here are sample steps and code so you can first test to see how this works:
- Create a new, blank InfoPath form - Add 2 Date Picker controls named: date1 and date2 - Add a text box control named: txtElapsedDays - From the Tools menu, choose Form Options, select the Advanced tab and make sure the "Form Code Language" is set to: VBScript - Click OK - Right-click on date1, choose Properties, click the Data Validation button, select OnAfterChange from the Events box and click Edit - You should see the following:
Sub msoxd_my_date1_OnAfterChange(eventObj)
' Write code here to restore the global state.
If eventObj.IsUndoRedo Then ' An undo or redo operation has occurred and the DOM is read-only. Exit Sub End If
' A field change has occurred and the DOM is writable. Write code here to respond to the changes.
End Sub
- Just before the "End Sub" line, add the following code:
If eventObj.Operation = "Insert" Then Dim objDate2 Dim objElapsedDays Set objDate2 = XDocument.DOM.selectSingleNode("//my:myFields/my:date2") Set objElapsedDays = XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays") If objDate2.text <> "" Then objElapsedDays.text = CalcDays(eventObj.Site.text, objDate2.text) End If End If
- Switch back to the InfoPath designer, right-click on date2, choose Properties, click the Data Validation button, select OnAfterChange from the Events box and click Edit - This will put you back in the Script Editor but with the same basic code structure for date2. Just before the "End Sub" line in date2 add the following:
If eventObj.Operation = "Insert" Then Dim objDate1 Dim objElapsedDays Set objDate1 = XDocument.DOM.selectSingleNode("//my:myFields/my:date1") Set objElapsedDays = XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays") If objDate1.text <> "" Then objElapsedDays.text = CalcDays(objDate1.text, eventObj.Site.text) End If End If
- While you are still in the Script Editor, scroll all the way down past the last "End Sub", make sure your cursor is in the "blank area" and add the following function:
Function CalcDays(date1, date2) CalcDays = DateDiff("d", date1, date2) End Function
- Save and close the Script Editor - Preview the form and select a date from each control - does it work as you need?
Scott L. Heim Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights
Blackduke - 04 Sep 2005 00:56 GMT sorry thourght i replied to this, I do not know which to use script or code! I have never done it before I will have to go by your suggestion.
Thanks
SRY for the delay
>No problem - so would you like managed code or script? And then what >language do you prefer? [quoted text clipped - 3 lines] > >This posting is provided "AS IS" with no warranties, and confers no rights. Scott L. Heim [MSFT] - 06 Sep 2005 20:07 GMT Hi,
You did and I subsequently provided sample steps and script. Let me know if it works for you!
Best regards,
Scott L. Heim Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights
Blackduke - 06 Sep 2005 23:07 GMT Thanks I have found the post you did, I will try this tomorrow
and I am sure you won't get flamed :)
Thanks for the work I hope it works
Robbie
I am sure I'll get "flamed" for this <G> but for date calculations, I much prefer VBScript. So here are sample steps and code so you can first test to see how this works:
- Create a new, blank InfoPath form - Add 2 Date Picker controls named: date1 and date2 - Add a text box control named: txtElapsedDays - From the Tools menu, choose Form Options, select the Advanced tab and make sure the "Form Code Language" is set to: VBScript - Click OK - Right-click on date1, choose Properties, click the Data Validation button, select OnAfterChange from the Events box and click Edit - You should see the following:
Sub msoxd_my_date1_OnAfterChange(eventObj)
' Write code here to restore the global state.
If eventObj.IsUndoRedo Then ' An undo or redo operation has occurred and the DOM is read-only. Exit Sub End If
' A field change has occurred and the DOM is writable. Write code here to respond to the changes.
End Sub
- Just before the "End Sub" line, add the following code:
If eventObj.Operation = "Insert" Then Dim objDate2 Dim objElapsedDays Set objDate2 = XDocument.DOM.selectSingleNode("//my:myFields/my:date2") Set objElapsedDays = XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays") If objDate2.text <> "" Then objElapsedDays.text = CalcDays(eventObj.Site.text, objDate2.text) End If End If
- Switch back to the InfoPath designer, right-click on date2, choose Properties, click the Data Validation button, select OnAfterChange from the Events box and click Edit - This will put you back in the Script Editor but with the same basic code structure for date2. Just before the "End Sub" line in date2 add the following:
If eventObj.Operation = "Insert" Then Dim objDate1 Dim objElapsedDays Set objDate1 = XDocument.DOM.selectSingleNode("//my:myFields/my:date1") Set objElapsedDays = XDocument.DOM.selectSingleNode("//my:myFields/my:txtElapsedDays") If objDate1.text <> "" Then objElapsedDays.text = CalcDays(objDate1.text, eventObj.Site.text) End If End If
- While you are still in the Script Editor, scroll all the way down past the last "End Sub", make sure your cursor is in the "blank area" and add the following function:
Function CalcDays(date1, date2) CalcDays = DateDiff("d", date1, date2) End Function
- Save and close the Script Editor - Preview the form and select a date from each control - does it work as you need?
Scott L. Heim Microsoft Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights
|
|
|