Calculating number of days between dates using JScript

B

Bobby

I have examined Mr. Wong-A-Ton's examples of how to do this using VBScript,
but I need to do it using InfoPath's JScript because I already have alot of
other code written in JScript that I do not want to rewrite. Is there an
example of how to do this in InfoPath 2003 using JScript?
 
S

S.Y.M. Wong-A-Ton

Small correction: It's Ms. not Mr. :) I haven't written an article on
calculating date differences in JScript, just time differences
(http://enterprise-solutions.swits.n...g-time-calculating-differences&c=infopath2003),
but it will give you an idea how to go about things. Also take a look at this
very informative article on date arithmetic using JScript
(http://msdn2.microsoft.com/en-us/library/Aa239571(VS.60).aspx). And a search
in this newsgroup (search on "date difference") should return many code
snippets, since this question has been asked countless times.
 
B

Bobby

My apologies Ms. Wong-A-Ton, I hope I have not offended you. Thank you very
much for the reply. I have scavanged through all of the code examples I can
find and the documentation you sent me, however, I am still at a loss for
figuring out something that is probably very simple. Some of the code
examples appear to have arguments that are not compatible with InfoPath 2003
and I do not know what the arguments should be. Specifically, I do think the
'DateTime' object is valid, nor the 'TimeSpan' object.

My code so far:

function msoxd_my_IFSPDate::OnAfterChange(eventObj)
{
if (eventObj.IsUndoRedo)
return;

var date1Node = XDocument.DOM.selectSingleNode( "/my:IFSP/my:IFSPDate" );
var date2Node = XDocument.DOM.selectSingleNode(
"/my:IFSP/my:ReferralDate" );
var diffNode = XDocument.DOM.selectSingleNode( "/my:IFSP/my:Difference"
);

if( date1Node != null && date2Node != null && diffNode != null )
{
try
{
DateTime dt1 = GetDate( date1Node.text );
DateTime dt2 = GetDate( date2Node.text );

TimeSpan ts = dt2 - dt1;
diffNode.txt = ts.Days.ToString();
}
catch( FormatException ) {}
}
}

I am using the following function to put the date in ISO8601 format and it
is working for my other functions.

function GetDateString(dateObj)
{
var yyyy = dateObj.getFullYear();
var mm = dateObj.getMonth() + 1;
var dd = dateObj.getDate();

if (mm < 10)
mm = "0" + mm;

if (dd < 10)
dd = "0" + dd;

return yyyy + "-" + mm + "-" + dd;

}

I am not sure what I am doing wrong, since before this week, I have never
worked with this level of programming before :( Thank you for everything you
do for all of us lost in code!
 
S

S.Y.M. Wong-A-Ton

That's okay, Bobby. Many people make that mistake...

DateTime and TimeSpan objects are used in .NET, not scripting. You are
mixing programming languages together, which is why you are getting back
those errors.

You need to use the Date object (see
http://msdn2.microsoft.com/en-us/library/cd9w2te4.aspx) and getTime() method
on the Date object (see
http://msdn2.microsoft.com/en-us/library/7hcawkw2.aspx) to get the difference
between dates. The basic steps are:

1. Construct a Date object for both the start and end date using the yyyy,
mm, and dd parts of the InfoPath date as in

var dateObj1 = new Date(year, month, date) ;

where you would get year, month, and date from your InfoPath date string
(see
http://enterprise-solutions.swits.n...hp?t=date-time-basics-infopath&c=infopath2003).

2. Call getTime() on each one of those Date objects you created. This will
give you the amount of milliseconds since 1970 for each date (see
http://msdn2.microsoft.com/en-us/library/7hcawkw2.aspx).

var t1 = dateObj1.getTime();

3. Subtracting the two time results will give you the difference between the
two dates in milliseconds.

4. You then have to convert milliseconds to days and you're done.

Last time I tried using getTime(), it was returning inaccurate results, so
that might be something to look out for.

Hope this helps.
 
B

Bobby

I have read everything I can find, I just do not understand enough about this
to get it to work. Nonetheless, thank you very much for your assistance.
 

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