Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
109
rated 0 times [  110] [ 1]  / answers: 1 / hits: 42858  / 12 Years ago, thu, november 8, 2012, 12:00:00

I am trying to check if a date of format mm.dd.yyyy is greater than today and less than the date after 6 months from today.



Here is my code:



var isLinkExpiryDateWithinRange = function(value) {
var monthfield = value.split('.')[0];
var dayfield = value.split('.')[1];
var yearfield = value.split('.')[2];
var inputDate = new Date(yearfield, monthfield - 1, dayfield);
var today = new Date();
today = new Date(today.getFullYear(), today.getMonth(), today.getDate());
alert(inputDate > today);//alert-> true
var endDate = today;
endDate.setMonth(endDate.getMonth() + 6);
alert(inputDate > today);//alert-> false
if(inputDate > today && inputDate < endDate) {
alert('1');
} else {
alert('2');/always alert it
}
}


If I execute isLinkExpiryDateWithinRange('12.08.2012') I wish it will show 1 as this is within the range, but it is displaying 2. Moreover the first alert is showing true and the second one false.



Can anyone please explain what is happening?


More From » date

 Answers
8

Change:



var endDate = today;


to:



var endDate = new Date(today);


See the posts here for how objects are referenced and changed. There are some really good examples that help explain the issue, notably:




Instead, the situation is that the item passed in is passed by value.
But the item that is passed by value is itself a reference.




JSFiddle example


[#82102] Wednesday, November 7, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;