Quantcast
Channel: Detecting an "invalid date" Date instance in JavaScript - Stack Overflow
Browsing latest articles
Browse All 57 View Live

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 Article



Answer 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 Article

Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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 Article


Answer 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 Article


Answer by abhirathore2006 for Detecting an "invalid date" Date instance in...

shortest answer to check valid date if(!isNaN(date.getTime()))

View Article

Answer 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 Article

Answer by Nick Taras for Detecting an "invalid date" Date instance in JavaScript

For Angular.js projects you can use: angular.isDate(myDate);

View Article

Answer 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 Article


Answer by Joel Kornbluh for Detecting an "invalid date" Date instance in...

function isValidDate(date) { return !! (Object.prototype.toString.call(date) === "[object Date]" && +date); }

View Article

Answer 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 Article


Answer 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 Article

Answer 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 Article


Answer 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 Article

Answer 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 Article

Answer 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

Answer by user1296274 for Detecting an "invalid date" Date instance in...

This just worked for me new Date('foo') == 'Invalid Date'; //is true However this didn't work new Date('foo') === 'Invalid Date'; //is false

View Article


Answer by Mina Gabriel for Detecting an "invalid date" Date instance in...

you can convert your date and time to milliseconds getTime() this getTime() Method return Not a Number NaN when not valid if(!isNaN(new Date("2012/25/255").getTime())) return 'valid date time'; return...

View Article


Answer by Denis Ryzhkov for Detecting an "invalid date" Date instance in...

For int 1-based components of a date: var is_valid_date = function(year, month, day) { var d = new Date(year, month - 1, day); return d.getFullYear() === year && (d.getMonth() + 1) === month...

View Article

Answer by Yves M. for Detecting an "invalid date" Date instance in JavaScript

You can simply use moment.js Here is an example: var m = moment('2015-11-32', 'YYYY-MM-DD'); m.isValid(); // false The validation section in the documentation is quite clear. And also, the following...

View Article

Answer by kam for Detecting an "invalid date" Date instance in JavaScript

Date object to string is more simple and reliable way to detect if both fields are valid date. e.g. If you enter this "-------" to the date input field. Some of the above answers won't work....

View Article


Answer by Matt Campbell for Detecting an "invalid date" Date instance in...

Would like to mention that the jQuery UI DatePicker widget has a very good date validator utility method that checks for format and validity (e.g., no 01/33/2013 dates allowed). Even if you don't want...

View Article

Answer by Michael Goldshmidt for Detecting an "invalid date" Date instance in...

IsValidDate: function(date) { var regex = /\d{1,2}\/\d{1,2}\/\d{4}/; if (!regex.test(date)) return false; var day = Number(date.split("/")[1]); date = new Date(date); if (date && date.getDate()...

View Article

Answer by Ash Clarke for Detecting an "invalid date" Date instance in JavaScript

My solution is for simply checking whether you get a valid date object: Implementation Date.prototype.isValid = function () { // An invalid date object returns NaN for getTime() and NaN is the only //...

View Article

Answer by Dex for Detecting an "invalid date" Date instance in JavaScript

None of these answers worked for me (tested in Safari 6.0) when trying to validate a date such as 2/31/2012, however, they work fine when trying any date greater than 31. So I had to brute force a...

View Article



Answer by John for Detecting an "invalid date" Date instance in JavaScript

None of the above solutions worked for me what did work however is function validDate (d) { var date = new Date(d); var day = ""+date.getDate(); if( day.length == 1)day = "0"+day; var month = "" +(...

View Article

Answer by Yusef Mohamadi for Detecting an "invalid date" Date instance in...

you can check the valid format of txDate.value with this scirpt. if it was in incorrect format the Date obejct not instanced and return null to dt . var dt = new Date(txtDate.value) if (isNaN(dt)) And...

View Article

Answer by Jingguo Yao for Detecting an "invalid date" Date instance in...

I use the following code to validate values for year, month and date. function createDate(year, month, _date) { var d = new Date(year, month, _date); if (d.getFullYear() != year || d.getMonth() !=...

View Article

Answer by Borgar for Detecting an "invalid date" Date instance in JavaScript

Here's how I would do it: if (Object.prototype.toString.call(d) === "[object Date]") { // it is a date if (isNaN(d.getTime())) { // d.valueOf() could also work // date is not valid } else { // date is...

View Article


Answer by Ash for Detecting an "invalid date" Date instance in JavaScript

Instead of using new Date() you should use: var timestamp = Date.parse('foo'); if (isNaN(timestamp) == false) { var d = new Date(timestamp); } Date.parse() returns a timestamp, an integer representing...

View Article

Detecting an "invalid date" Date instance in JavaScript

I'd like to tell the difference between valid and invalid date objects in JS, but couldn't figure out how: var d = new Date("foo"); console.log(d.toString()); // shows 'Invalid Date'...

View Article

Answer by S. Esteves for Detecting an "invalid date" Date instance in JavaScript

Pure JavaScript solution:const date = new Date(year, (+month-1), day);const isValidDate = (Boolean(+date) && date.getDate() == day);Also works on leap years!Credit to...

View Article


Answer by Stefan Rein for Detecting an "invalid date" Date instance in...

No one has mentioned it yet, so Symbols would also be a way to go:Symbol.for(new Date("Peter")) === Symbol.for("Invalid Date") // trueSymbol.for(new Date()) === Symbol.for("Invalid Date") //...

View Article


Answer by duan for Detecting an "invalid date" Date instance in JavaScript

If you use io-ts, you can use the decoder DateFromISOString directly.import { DateFromISOString } from 'io-ts-types/lib/DateFromISOString'const decoded =...

View Article

Answer by iClyde for Detecting an "invalid date" Date instance in JavaScript

pretty old topic...Try something like this:if (!('null' === JSON.stringify(new Date('wrong date')))) console.log('correct');else console.log('wrong');

View Article

Answer by ßãlãjî for Detecting an "invalid date" Date instance in JavaScript

Why i Suggest moment.jsit is very popular librarysimple to solve all date and time,format,timezone problemseasy to check string date valid or notvar date = moment("2016-10-19");date.isValid()we can't...

View Article

Answer by Steven Spungin for Detecting an "invalid date" Date instance in...

After reading every answer so far, I am going to ofter the most simple of answers.Every solution here mentions calling date.getTime(). However, this is not needed, as the default conversion from Date...

View Article


Answer by Carsten Führmann for Detecting an "invalid date" Date instance in...

I rarely recommend libraries when one can do without. But considering the plethora of answers so far it seems worth pointing out that the popular library "date-fns" has a function isValid.

View Article

Answer by Vijay Jagdale for Detecting an "invalid date" Date instance in...

Why am I writing a 48th answer after so many have tried before me?For checking if it is Date Object and then a valid date object - dead simple:return x instanceof Date && !!x.getDate();Now for...

View Article


Answer by LocV's Nest for Detecting an "invalid date" Date instance in...

Maybe I'm late but I have a solution.const isInvalidDate = (dateString) => JSON.stringify(new Date(dateString)) === 'null';const invalidDate = new...

View Article

--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article


--- Article Not Found! ---

*** *** *** RSSing Note: Article is missing! We don't know where we put it!!. *** ***

View Article

Answer by Jordan Szymczyk for Detecting an "invalid date" Date instance in...

NaN is falsy.invalidDateObject.valueOf() is NaN.const d = new Date('foo');if (!d.valueOf()) { console.error('Not a valid date object');}else { // act on your validated date object}Even though valueOf()...

View Article

Answer by Hoyeon Kim for Detecting an "invalid date" Date instance in JavaScript

Here I came up with a solution that might be helpful for those looking for a test function that can check whether it's given yyyy/mm/dd or mm/dd/yyyy also with serveral symbols such as '/', '-',...

View Article

Answer by King Friday for Detecting an "invalid date" Date instance in...

const isDate = (str) => String(new Date(str)) !== 'Invalid Date'so tonight i'm gonna party up to isDate('12/31/999999')

View Article


Answer by Andrey Patseiko for Detecting an "invalid date" Date instance in...

You can try something like this:const isDate = (val) => !isNaN(new Date(val).getTime());

View Article

Browsing latest articles
Browse All 57 View Live




Latest Images