JavaScript - Best way to Detect Invalid Date Across Multiple Browsers

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.
894359Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try...

IF ( isNAN(lastvisit) == False )
--- VALID DATE
894359Author 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 ExpertCommented:
So what you need in return.
Determine the Perfect Price for Your 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 with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

894359Author Commented:
Just trying to find the best way to detect an invalid date.
Dave BaldwinFixer of ProblemsCommented:
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)".
Julian HansenCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
894359Author 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())
Julian HansenCommented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.