Answer by Soubriquet for Detecting an "invalid date" Date instance in JavaScript
Simple and elegant solution: const date = new Date(`${year}-${month}-${day} 00:00`) const isValidDate = (Boolean(+date) && date.getDate() == day) sources: [1]...
View ArticleAnswer by iwatakeshi for Detecting an "invalid date" Date instance in JavaScript
Yet another way to check whether the date is a valid date object: const isValidDate = (date) => typeof date === 'object' && typeof date.getTime === 'function' && !isNaN(date.getTime())
View ArticleAnswer by Jason Foglia for Detecting an "invalid date" Date instance in...
I seen some answers that came real close to this little snippet. JavaScript way: function isValidDate(dateObject){ return new Date(dateObject).toString() !== 'Invalid Date'; } isValidDate(new...
View ArticleAnswer by saaj for Detecting an "invalid date" Date instance in JavaScript
Date.prototype.toISOString throws RangeError (at least in Chromium and Firefox) on invalid dates. You can use it as a means of validation and may not need isValidDate as such (EAFP). Otherwise it's:...
View ArticleAnswer by Sebastien H. for Detecting an "invalid date" Date instance in...
Too many complicated answers here already, but a simple line is sufficient (ES5): Date.prototype.isValid = function (d) { return !isNaN(Date.parse(d)) } ; or even in ES6 : Date.prototype.isValid = d...
View ArticleAnswer by Saurabh Gupta for Detecting an "invalid date" Date instance in...
So I liked @Ask Clarke answer with little improvement by adding try catch block for dates which cannot go through var d = new Date(d) - function checkIfDateNotValid(d) { try{ var d = new Date(d);...
View ArticleAnswer by rainabba for Detecting an "invalid date" Date instance in JavaScript
date.parse(valueToBeTested) > 0 is all that's needed. A valid date will return the epoch value and an invalid value will return NaN which will fail > 0 test by virtue of not even being a number....
View ArticleAnswer by Zon for Detecting an "invalid date" Date instance in JavaScript
A ready function based on top rated answer: /** * Check if date exists and is valid. * * @param {String} dateString Date in YYYY-mm-dd format. */ function isValidDate(dateString) { var isValid = false;...
View ArticleAnswer by Dustin Poissant for Detecting an "invalid date" Date instance in...
Date.valid = function(str){ var d = new Date(str); return (Object.prototype.toString.call(d) === "[object Date]" && !isNaN(d.getTime())); }...
View ArticleAnswer by abhirathore2006 for Detecting an "invalid date" Date instance in...
shortest answer to check valid date if(!isNaN(date.getTime()))
View ArticleAnswer by Greg Finzer for Detecting an "invalid date" Date instance in...
This flavor of isValidDate uses a regular expression that handles leap years: function isValidDate(value) { return...
View ArticleAnswer by Nick Taras for Detecting an "invalid date" Date instance in JavaScript
For Angular.js projects you can use: angular.isDate(myDate);
View ArticleAnswer by kiranvj for Detecting an "invalid date" Date instance in JavaScript
function isValidDate(strDate) { var myDateStr= new Date(strDate); if( ! isNaN ( myDateStr.getMonth() ) ) { return true; } return false; } Call it like this isValidDate(""2015/5/2""); // => true...
View ArticleAnswer by Joel Kornbluh for Detecting an "invalid date" Date instance in...
function isValidDate(date) { return !! (Object.prototype.toString.call(date) === "[object Date]" && +date); }
View ArticleAnswer by pixelbacon for Detecting an "invalid date" Date instance in JavaScript
Generally I'd stick with whatever Date implantation is in the browser stack. Which means you will always get "Invalid Date" when calling toDateString() in Chrome, Firefox, and Safari as of this reply's...
View ArticleAnswer by wanglabs for Detecting an "invalid date" Date instance in JavaScript
This function validates a string date in digit formats delimited by a character, e.g. dd/mm/yyyy, mm/dd/yyyy /* Param : 1)the date in string data type 2)[optional - string - default is "/"] the date...
View ArticleAnswer by zVictor for Detecting an "invalid date" Date instance in JavaScript
I combined the best performance results I found around that check if a given object: is a Date instance (benchmark here) has a valid date (benchmark here) The result is the following: function...
View ArticleAnswer by dolphus333 for Detecting an "invalid date" Date instance in JavaScript
The selected answer is excellent, and I'm using it as well. However, if you're looking for a way to validate user date input, you should be aware that the Date object is very persistent about making...
View ArticleAnswer by Yaseen for Detecting an "invalid date" Date instance in JavaScript
I've written this function. Pass it a string parameter and it will determine whether it's a valid date or not based on this format "dd/MM/yyyy". here is a test input: "hahaha",output: false. input:...
View ArticleAnswer by Dhayalan for Detecting an "invalid date" Date instance in JavaScript
var isDate_ = function(input) { var status = false; if (!input || input.length <= 0) { status = false; } else { var result = new Date(input); if (result == 'Invalid Date') { status = false; } else {...
View Article