new Date() producing valid date from invalid input string

how to validate input type=date in javascript
javascript new date returns invalid date
invalid date validation in jquery
from date to date validation in javascript
simple date validation in javascript
moment js check if date is valid
moment isvalid
moment test if string is valid date

Passing a string to new Date is the same as using Date.parse.

When a non-standard date string is passed, the result is implementation-dependent; the browser can do whatever it wants, including guessing. On Chrome, your input results in a date, but not on Firefox (NaN is returned).

test isn't part of a date string, so it looks like Chrome just parses the 2:

console.log(new Date('2'));
console.log(new Date('1'));
console.log(new Date('0'));

DateTime.parse should throw an error on invalid date � Issue , Put this line and run: DateTime pDate = DateTime.parse("20130547"); It will return: where invalid inputs are anything that isn't a valid date in a recognized format. option of making the parser reject strings which don't have an explicit timezone in it. DateTime.parse() should throw an exception when incorrect values are� I'm receiving a date string from an API, and it is formatted as yyyy-mm-dd. I am currently using a regex to validate the string format, which works ok, but I can see some cases where it could be a correct format according to the string but actually an invalid date. i.e. 2013-13-01, for example.


As per MDN:

The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm.

Google Chrome tries to parse your date and picks up the number as a month. Other browsers may reject the passed value or return something totally different.

Hence please consider the following note:

Parsing of date strings with the Date constructor (and Date.parse(), which works the same way) is strongly discouraged due to browser differences and inconsistencies.

MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#Parameters

Validation, new Date(2013, 25, 14). userInvalidated : A date created explicitly as invalid, such as moment.invalid() . unusedTokens : array of format substrings not found in the input string All of the following produce invalid moments: * invalid.add( unit, value) * another.add(invalid) * invalid.clone() * invalid.diff(another) * invalid. Detecting an “invalid date” Date instance in JavaScript - The Date.parse() method parses a string representation of a date, and returns the number of milliseconds.


From mdn:

A string representing a simplification of the ISO 8601 calendar date extended format (other formats may be used, but results are implementation-dependent).

You're encountering an implementation-specific attempt to parse your string as a date. It is not recommended to depend on this behaviour.

Date Validation Using Moment.js, Should those fail, it passes the input to the Date(string) constructor. Due to inconsistent browser implementation of dates, your best bet is to supply your preferred format(s) to the Moment.js () constructor: It returns a boolean: true for valid, false for invalid. It will produce an object much like this one: 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.


Because your test text includes a number. See, it's interpreting a number like 1 as January, 2 as February,3 as March, and so on.

new Date( "3")

will give you

Thu Mar 01 2001 00:00:00 GMT+0530 (India Standard Time)

If you use text with no number, e.g. new Date("test") it will throw invalid date.

Form Validation: Date and Time < JavaScript, Using JavaScript to validate date and time input fields. !form.startdate.value. match(re)) { alert("Invalid date format: " + alert("Invalid value for year: " + regs[3] + " - must be between 1902 and " + (new Date()). 1 through 3 containing the day, month and year components of the input string. Modular checkDate() function. Given a date, check if it is valid or not. It may be assumed that the given date is in range from 01/01/1800 to 31/12/9999. Examples : Input : d = 10, m = 12, y = 2000 Output : Yes The given date 10/12/2000 is valid Input : d = 30, m = 2, y = 2000 Output : No The given date 30/2/2000 is invalid. The February month cannot have 30 as day.


<input type="date">, input elements of type="date" create input fields that let the user enter a date, either in Format of a valid date string in Date and time formats used in HTML. or enter an invalid date in browsers where the input falls back to type text (like We create a new <input> element, try setting its type to date , then� Finally, IE 11 has begun to emit Unicode RTL marks into the datetime strings emitted by it’s various toLocaleString() methods, which causes “Invalid Date” to appear when attempting to create new Date objects with the Date(dateString) constructor and the Date.parse(dateString) method. The newly embedded Unicode characters may cause your


Check If a String Is a Valid Date in Java, Have a look at different ways to check if a String is a valid date in Java. The canonical reference for building a production grade API with Spring. I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 The input contains the date in a valid format, such as MM/DD/YYYY� The required expression argument is a Variant containing a date expression or string expression recognizable as a date or time. Remarks In Windows, the range of valid dates is January 1, 100 A.D., through December 31, 9999 A.D.; the ranges vary among operating systems.


Date and Time Input / Output — Snowflake Documentation, Using the Correct Number of Digits with Format Elements AUTO Detection of Integer-stored Date, Time, and Timestamp Values tables below); however, some formats might produce ambiguous results, which can cause Snowflake detects and processes the following formats for date, time, and timestamp input strings. Hi Everyone, I'm facing a huge difficulty in trying to convert the empty string into a date from a flat file to a SQL table. The flat file is the fixed