Page 1 of 1

Difference between dates

Posted: Tue Sep 20, 2016 1:42 pm
by ppbedz
I am trying to calculate the number of days between 2 dates using javascript. I used a textbook example to code the OnChange property. I am enclosing JavaScript, debug alerts, and error message. The alerts indicate my variables contain dates, but Profound keeps telling me that "getTime" is not a function. Can you please assist? Thank you.

Re: Difference between dates

Posted: Tue Sep 20, 2016 2:39 pm
by Glenn
Patti,

An important thing to understand about JavaScript is that it is a loosely typed language.

What this means is that even though you start with a Date object.

Code: Select all

var date1 = new Date();
This code changes it to a character variable since that's what the get() API returns. A character variable doesn't have the getTime method which is why you get the error.

Code: Select all

date1 = get("DueDate.1");
What you want to do is convert the result of the get() API to a Date object. I changed your code to what's below and it worked for me (you would want to put the get("") API calls in the place I put the hard-coded dates).

Code: Select all

var date1 = new Date("09/05/16");
var date2 = new Date("09/21/16");
I got a response of -16. Does the above make sense?

Glenn

Re: Difference between dates

Posted: Tue Sep 20, 2016 2:46 pm
by ppbedz
Yes, that makes sense. I modified my code and it works now.. Thanks so much!

Re: Difference between dates

Posted: Tue Sep 20, 2016 2:57 pm
by ppbedz
Glenn,

How would I test for no value in the completion date? If the test has not been completed yet, I want to compare the due date to the current date to determine the days late.

Thank you,
Patti

Re: Difference between dates

Posted: Tue Sep 20, 2016 3:05 pm
by Glenn
Patti,

I would do something like the following:

Code: Select all

if (get("CompDate.1") != "") {
  // do something here
}
Also, as an aside, you can insert a 'debugger' statement in your function and if you have the developer's tools open in your browser it will force the JavaScript debugger to start and it will halt on that line. For example, below is the code I used to answer your question. Note the 'debugger' statement.

Code: Select all

var date1 = new Date("09/05/16");
var date2 = new Date("09/21/16");
var msec_per_day = 1000 * 60 * 60 * 24;
var diff = 0;
var numDays = 0;
 
debugger;

diff = (date1.getTime() - date2.getTime()) / msec_per_day;
alert(diff);
Glenn

Re: Difference between dates

Posted: Tue Sep 20, 2016 3:12 pm
by ppbedz
Great! Thanks again!

Re: Difference between dates

Posted: Tue Sep 20, 2016 3:26 pm
by ppbedz
Sorry.. one more thing. I modified the code to use the current date in the calculation if the completed date is blank. The math gives me an extra day when I use the current date. For example, if the request was due Sunday the 18th and today is Tuesday the 20th, it tells me the request is 3 days late instead of 2? Any idea why? If I change the completed date to today's date, the math is correct (I am two days late).

Re: Difference between dates

Posted: Tue Sep 20, 2016 3:41 pm
by Glenn
Patti,

I'm not sure. You'd have to look at the exact results from each line.

I can take a guess that it's related to the time portion of the date. If you use 'date1 = new Date()' you would get something like 'Tue Sep 20 2016 15:32:22 GMT-0400' whereas 'date1 = new Date("09/20/16")' will get you 'Tue Sep 20 2016 00:00:00 GMT-0400'. Since your code uses the time values and Math.round that might be causing your issue. I would perhaps try putting a hidden field on the screen that is bound to an RPG variable that contains today's date in the same format as the others and sue that in the Date() API.

Glenn

Re: Difference between dates

Posted: Wed Sep 21, 2016 9:21 am
by ppbedz
The hidden field solved the problem. Thank you.