I am reading a date from local storage and then need to compare it to another date but it does not work. I think I need to typecast it but am very rusty with Javascript, please have a look, my code is not much:
// ****** ### IMPORTANT: The below line returns "2011,3,20" ### *******
var da_expiry_date = localStorage['list_expiry_date'];
var today_date = new Date();
var future_date = new Date();
future_date.setFullYear(da_expiry_date+2);
alert (future_date+"\n"+today_date);
if (future_date>today_date) {
alert("1");
}
else {
alert("2");
}
Thanks in advance!
If you're comment at the top is correct and
da_expiry_date
is returning a string, then you will need to typecast it to date before you can do anything meaningful. The easiest way to do this is with oneof the methods listed here. The problem with converting a string to a date is knowing the format; if you have all the pieces separated you can just use one of the other date constructors. :DAs it is, you code is trying to take the string
"2011,3,20"
, add the number2
to it, and set it as the full year offuture_date
. The browser ends up converting 2 to a string and concatenating that to the end, giving you"2011,3,202"
.setFullYear
expects an int instead of a string, so it can't do its job.You probably want that line switched to this after you've got your date properly converted: