Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid string is converted to date

Hi I have a string 'Windows-10' which when I try to parse via new Date() constructor it is getting parsed and gives a completely valid date as 'Mon Oct 01 2001 00:00:00 GMT+0530 (India Standard Time)'

I dont know why is this happening I also tried it with other Windows string such as 'Windows-7', 'Windows-99' etc. and they all parse easily.

like image 923
Vibhanshu Biswas Avatar asked Nov 02 '18 10:11

Vibhanshu Biswas


People also ask

What is Transformation evaluation error in Informatica?

This error occurs when data transferred to MD5 function is not of String or Binary data type. 1) For Solution, enter CR with a Workaround if a direct Solution is not available.


1 Answers

The behavior you witnessed is implementation-specific, which for the one-arg Date(value) constructor is covered by the ECMA-262 spec in chapter 20.3.2.2. Your example will walk through to step 3.b.ii.1. which states that the string will be parsed according to the rules laid out in chapter 20.3.3.2 for the Date.parse(string) method. That method specification defines:

The function first attempts to parse the format of the String according to the rules (including extended years) called out in Date Time String Format (20.3.1.16). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

Because your strings obviously do not comply with the Date Time String Format the browser falls back to its implementation-specific algorithm. My Chrome 70, e.g., returns a Date object for the current time which corresponds to calling the no-args Date() constructor. IE11 on the other hand parses the string to NaN and returns a Date object with an "invalid date" value.

like image 94
altocumulus Avatar answered Oct 07 '22 21:10

altocumulus