标签云

微信群

扫码加入我们

WeChat QR Code

What is an easy way to check if a value is a valid date, any known date format allowed. For example I have the values 10-11-2009, 10/11/2009, 2009-11-10T07:00:00+0000 which should all be recognized as date values, and the values 200, 10, 350, which should not be recognized as a date value. What is the simplest way to check this, if this is even possible? Because timestamps would also be allowed.


Duplicate: stackoverflow.com/questions/1353684/…

2019年06月25日13分32秒

Be careful with this as it will still return return for invalid dates in February, for example: 2013-02-31

2019年06月25日13分32秒

Another reason to be careful - numbers parse as dates. Date.parse("4.3") is 986270400000.

2019年06月26日13分32秒

I tried a fiddle with both of the 'caution' cases above: console.log(Date.parse("2013-02-31")); console.log(Date.parse("4.3")); and in both cases (on firefox) it returned NaN, so for me, Date.parse seems OK (I'm prevalidating for dashes and correct length before the parse anyway).

2019年06月26日13分32秒

Cloudranger - In Chrome it parses it into a date.

2019年06月26日13分32秒

Sorry Asmor, this is not a correct answer. Date.parse will parse any string with a number it in.

2019年06月26日13分32秒

how exactly should one use this? moment("whatever 123").isValid() returns true.

2019年06月25日13分32秒

krivar It's best to use it like this: moment("06/22/2015", "DD/MM/YYYY", true).isValid(); with the expected date format explicitly provided and with an argument true meaning strict checking. I updated my answer with more info and better examples.

2019年06月26日13分32秒

Same though, but I'm validating user input and I don't know the expected date format...

2019年06月25日13分32秒

JanVanderHaegen If assumptions can be made that 3/4/5 is a valid date and so is April 1st 2015 then I would recommend adding those formats (and potentially much more) to an explicit list of supported formats. Note that 3/4/5 that you mentioned is ambiguous without an explicit format.

2019年06月26日13分32秒

This could be the best and most well maintained answer I have seen on SO

2019年06月26日13分32秒

I needed to return (new Date(date)).toString() !== "Invalid Date" for node. Also note that ? true : false is redundant. simply returning the expression will be enough here.

2019年06月25日13分32秒

In IE8, new Date(date) does not give you 'Invalid Date'.

2019年06月25日13分32秒

This is not a good answer. new Date(date) !== "Invalid Date" will always return true since the left expression will return a Date object with a timevalue of NaN, which can never be === to a string. Using == "works" due to type conversion. But since parsing of date strings is still largely implementation dependent, relying on it to parse random formats us seriously flawed.

2019年06月26日13分32秒

new Date("469") results inTue Jan 01469 00:00:00 GMT+0200 (EET)

2019年06月25日13分32秒

This is similar to the falsely accepted correct answer. isDate('Mac OS X 10.14.2') returns true here.

2019年06月26日13分32秒

I realize it's a couple years later, but isNan is not a function; incorrect case on the first instance.

2019年06月25日13分32秒

TimLewis Thanks. Fixed!

2019年06月25日13分32秒

Because the question is not wether to check if a string is a 'valid' date, but just checking wether a string represents a date format.

2019年06月25日13分32秒

Thizzer good point. I wouldn't mind fixing but on rereading your question I'm stumped because you say 10 shouldn't validate but that timestamps should be allowed.

2019年06月26日13分32秒

xD didn't even notice that, a question from my earlier days with date/timestamps. I will try to edit the question later today.

2019年06月25日13分32秒

great answer thanks

2019年06月25日13分32秒

why month-1 and then month+1?

2019年06月25日13分32秒

This doesn't work in current Chrome, possibly other browsers. I changed the string provided to EXAMPLE STRING 12345 and it's returns "Valid date".

2019年06月26日13分32秒

This is the one that worked for me, thanks!

2019年06月26日13分32秒

Still doesn't work for my example: isDate("  1")

2019年06月25日13分32秒

Tom You should check if the value contains white-space characters before determining if it's a date. Your issue is a special-case which needs to be handled by your controller logic.

2019年06月25日13分32秒

It's good, but it doesn't answer the question completely. Specifically, it doesn't work for '2009-11-10T07:00:00+0000', one of the examples given.

2019年06月25日13分32秒

Don't think this works as it should validateDate('2016-12-30T08:00:00.000Z') // false

2019年06月25日13分32秒

Regardless of whether or not this works, it could be simplified as return !isNaN(Date.parse(isoDate)) || isoDate == new Date(isoDate).toISOString().substr(0,10);

2019年06月25日13分32秒

"Works" for what values of date?

2019年06月26日13分32秒

...javascript?!

2019年06月25日13分32秒

This does not work for me. First the var in the condition does not parse, but if you remove that and try something like this:if (date = new Date("rubbish")) { alert(date);}Even if the date is rubbish, the alert will still execute as the Date function will return "Invalid Date" (on firefox at least) so the condition evaluates to true.I guess if some browsers return null on invalid date, then it would work on those browsers. There could be browser inconsistency here.

2019年06月26日13分32秒

Hacked SO account?

2019年06月25日13分32秒