days between dates

B

Blackduke

I have two date pickers can i populate another box with the amount of
days selected

thanks
 
S

Scott L. Heim [MSFT]

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.
 
B

Blackduke

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
 
S

Scott L. Heim [MSFT]

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.
 
S

Scott L. Heim [MSFT]

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.
 
B

Blackduke

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
 
S

Scott L. Heim [MSFT]

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.
 
B

Blackduke

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top