JavaScript - Best way to Detect Invalid Date Across Multiple Browsers

894359
894359 used Ask the Experts™
on
I'm trying to figure out if different browsers will return different messages/errors when a string is passed into a date function as per the below example where last[0] = "new visit":

var lastVisit = new Date(last[0]);

Open in new window


Chrome returns "Invalid Date" but not sure how robust this will be across all browsers if I'm using:

if(lastvisit != "Invalid Date")

Open in new window


I'm not sure if there is a better way to detect if I got a date format back or an invalid date? I can't do anything about the string being passed. It will happen the initial session a user hits the site.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
Please try...

IF ( isNAN(lastvisit) == False )
--- VALID DATE

Author

Commented:
I don't think that will work as the date object returns something like this Fri Dec 01 2017 22:01:37 GMT-0800 (Pacific Standard Time)
Pawan KumarDatabase Expert
Awarded 2016
Top Expert 2016

Commented:
So what you need in return.
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
Just trying to find the best way to detect an invalid date.
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

Commented:
JavaScript is very standardized across browsers.  There is no reason to think there will be differences.  Your biggest problem is that the date() function will try to return something that makes sense... even if it doesn't.  On this page https://www.w3schools.com/js/tryit.asp?filename=tryjs_date_new_string you can try all kinds of entries to see what happens.  When I entered '1923', it returned "Sun Dec 31 1922 16:00:00 GMT-0800 (Pacific Standard Time)".
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
This should work.
var dt = new Date('banana');
if (dt.getTime() === dt.getTime()) {
   console.log('Date is valid');
}
else {
   console.log('Date is invalid');
}

Open in new window

You can create a function on the prototype as well
Date.prototype.isValid = function() {
    return this.getTime() === this.getTime();
}
var dt = new Date('banana');
console.log(dt.isValid());

Open in new window

Author

Commented:
Hi Julian,
Wouldn't that always be true since you are comparing the exact same thing or was there a typo???

if (dt.getTime() === dt.getTime())
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
It is one of the things about the invalid Date - getTime when compared to itself does not evaluate to true. Tested back to IE9 and all other browsers.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial