Quantcast
Channel: Detecting an "invalid date" Date instance in JavaScript - Stack Overflow
Browsing all 58 articles
Browse latest View live

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 Bobby Thomas for Detecting an "invalid date" Date instance in...

return String(new Date('12/12/2002')) === "Invalid Date"

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


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

Do not depend on new Date().All of these but the last fails. They create date objects, just not on the date you expect.console.log(new Date(2023,02,35))console.log(new Date(2023,02,29))console.log(new...

View Article
Browsing all 58 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>