Date Calculation, Find Difference between two dates

S

Stu Morris

I am having a massive problem with this.
I have 2 date pickers & need to find the difference between the 2 dates.
Answer to be returned in days.

This is a challange, i don't think this can be done. Any takers???

This should be simple but this is the 4th forum and 4th book im into and
cant figure it.
 
S

Stu Morris

Frank,

I cant figure out this example at all, I have found a example of this in vb
code but i really need a Jscript one.

It really only has to be very simple two date pickers and one returned field
stating the number of days difference.

Please help

Stu
 
F

Franck Dauché

Hi,

I guess part of the problem comes from the fact that date pickers in
InfoPath give you ISO8601 dates.

If you use:
var date1 = XDocument.DOM.selectSingleNode( "//my:field1" ).text;
var date2 = XDocument.DOM.selectSingleNode( "//my:field2" ).text;
var dt1 = ISO8601ToDate(date1);
var dt2 = ISO8601ToDate(date2);

where:
function ISO8601ToDate(dateString)
{return new Date(dateString.split('-').reverse().join('/')) }

Then, you can do simple JScript date comparison as shown here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvid/html/msdn_vidateadd.asp

Hope that it helps.

Regards,

Franck Dauché
 
S

Stu Morris

Frank,

Thankyou, but i am still unsure, i really only need to find the difference
between two dates in days outputted in a text field. I am really struggling
with the conversions and such like.

Any simpler solutions, or more exact code??

Thanks Stu
 
S

Sandeep

Hi Stu Morris

This code is infopath Vbscript managed code.

Expl:
Field1 and field2 two are date pickers.
Field3 is textbox
Past this code

Sub CTRL18_5_OnClick(eventObj)

Dim nodeDate1
Dim nodeDate2
Dim dt1Val
Dim dt2Val
Dim daysElapsed

Set nodeDate1 = XDocument.DOM.selectSingleNode("/my:myFields/my:field1")
Set nodeDate2 = XDocument.DOM.selectSingleNode("/my:myFields/my:field2")

dt1Val = CDate(nodeDate1.Text)
dt2Val = CDate(nodeDate2.Text)

daysElapsed = DateDiff("d", dt1Val, dt2Val)

If daysElapsed < 3 Then

Msgbox "Date #2 must be at least 3 days in the future!"
XDocument.DOM.selectSingleNode("/my:myFields/my:field3").text = daysElapsed
End If

End Sub

I hope this will help U
 

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