2

When I pass "test 2" string in new Date(), I am getting an actual date, how?

I am trying to whether it is a date or not.

console.log(new Date("test 2"));
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Irfan Khan
  • 395
  • 4
  • 14
  • Where did you test this? If I try `var date=new Date( "test 2"); console.log(date);`, I get `Invalid Date`. – obscure Nov 29 '19 at 11:21
  • @obscure your example code works for me in Chrome -> `Thu Feb 01 2001 00:00:00 GMT+0000 (Greenwich Mean Time)` – phuzi Nov 29 '19 at 11:22
  • I tried on chrome – Irfan Khan Nov 29 '19 at 11:23
  • Are there any valid dates with only a single numeric character? I can't think of any. Sure, such a trivial test would reject your test string, but you'll need something considerably more robust. :( – enhzflep Nov 29 '19 at 11:25

4 Answers4

1

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'));

Essentially, this is undefined behavior, so strange results aren't surprising. Unless the passed string conforms to the format defined in the specification - that is, something like "2011-10-10" or "2011-10-10T14:48:00" or "2011-10-10T14:48:00.000+09:00", the results are unpredictable.

Consider instead figuring out what sort of string format you'd be expecting as an input, and then checking if the format is followed with a regular expression. If so, pass to new Date and see if it gives you a meaningful results; otherwise, don't.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
0

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

VisioN
  • 143,310
  • 32
  • 282
  • 281
0

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

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.

Stephen O'Flynn
  • 2,309
  • 23
  • 34
Abhigyan Gaurav
  • 1,854
  • 6
  • 28
  • 58