Difference between dates
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Difference between dates
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.
- Attachments
-
- dateissue.docx
- (141.37 KiB) Downloaded 78 times
- Glenn
- Profound Logic Staff Member
- Posts: 124
- Joined: Mon Apr 14, 2014 4:08 pm
- First Name: Glenn
- Last Name: Hopwood
- Company Name: Profound Logic Software
- State / Province: Ohio
- Country: United States
- Contact:
Re: Difference between dates
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.
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.
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).
I got a response of -16. Does the above make sense?
Glenn
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();
Code: Select all
date1 = get("DueDate.1");
Code: Select all
var date1 = new Date("09/05/16");
var date2 = new Date("09/21/16");
Glenn
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Re: Difference between dates
Yes, that makes sense. I modified my code and it works now.. Thanks so much!
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Re: Difference between dates
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
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
- Glenn
- Profound Logic Staff Member
- Posts: 124
- Joined: Mon Apr 14, 2014 4:08 pm
- First Name: Glenn
- Last Name: Hopwood
- Company Name: Profound Logic Software
- State / Province: Ohio
- Country: United States
- Contact:
Re: Difference between dates
Patti,
I would do something like the following:
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.
Glenn
I would do something like the following:
Code: Select all
if (get("CompDate.1") != "") {
// do something here
}
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);
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Re: Difference between dates
Great! Thanks again!
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Re: Difference between dates
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).
- Glenn
- Profound Logic Staff Member
- Posts: 124
- Joined: Mon Apr 14, 2014 4:08 pm
- First Name: Glenn
- Last Name: Hopwood
- Company Name: Profound Logic Software
- State / Province: Ohio
- Country: United States
- Contact:
Re: Difference between dates
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
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
-
- Experienced User
- Posts: 147
- Joined: Tue Jun 17, 2014 4:00 pm
- First Name: Patti
- Last Name: Bednarz
- Company Name: McGard
- State / Province: New York
- Country: United States
- Contact:
Re: Difference between dates
The hidden field solved the problem. Thank you.
Who is online
Users browsing this forum: No registered users and 1 guest