Reputation: 345
I am still fairly new with Javascript but I having a problem with dates. My code below works great in Chrome, Safari, and Opera but in Firefox & IE, shows "invalid date." I'm not sure why they aren't working when it works in the other browsers. Any help would be appreciated. Here is my code:
// Perl variable brings in the enrollment date
var enrollDate = new Date(user.joinDate);
var currentDate = new Date();
var expirationDate = new Date(enrollDate);
expirationDate.setDate(enrollDate.getDate()+7);
$('.hide-mailingAddress').addClass('hidden');
if (currentDate <= expirationDate) {
$('.show-mailingAddress').removeClass('hidden');
}
console.log("Join Date: " + enrollDate);
console.log("Current Date: " + currentDate);
console.log("Expiration Date: " + expirationDate);
Here is what Chrome, Safari, Opera outputs:
Join Date: Mon Dec 08 2014 00:00:00 GMT-0700 (MST)
Current Date: Mon Feb 02 2015 09:54:27 GMT-0700 (MST)
Expiration Date: Mon Dec 15 2014 00:00:00 GMT-0700 (MST)
Firefox & IE Outputs:
Join Date: Invalid Date
Current Date: Mon Feb 02 2015 09:49:41 GMT-0700 (MST)
Expiration Date: Invalid Date
Upvotes: 0
Views: 89
Reputation: 1
I suspect that user.joinDate is a quoted string:
Such as: ' "Mon Dec 08 2014 00:00:00 GMT-0700 (MST)" '
This extra set of quotes fails in Internet Explorer but is parsed fine by Chrome.
new Date('"Mon Dec 08 2014 00:00:00 GMT-0700 (MST)"')
Upvotes: 0
Reputation: 5903
Yes. In some versions of IE new date()
doesn't return as expected.
You may want to use:
var currentDate = new Date();
var finalDate = currentDate.getFullYear() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getDate();
Btw, currentDate.getMonth() + 1
is because it starts on 0
Upvotes: 2