Avatar of MJ
Flag for United States of America

asked on 

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.
Web DevelopmentChromeJavaScriptWeb Browsers

Avatar of undefined
Last Comment
Julian Hansen

8/22/2022 - Mon