2

When I giving value to Date() object by String and another by Numbers however parameters are similar, I got Different result in toLocaleString() method!

let date1 = new Date('1995 02 23');
date1.toLocaleString(); // "2/23/1995, 12:00:00 AM"

let date2 = new Date(1995, 02, 23)
date2.toLocaleString(); // "3/23/1995, 12:00:00 AM"

There is 1 month of difference between them.

Amir
  • 1,328
  • 2
  • 13
  • 27

2 Answers2

0

With your help I found out in number mode, month like Arrays count from 0 and if giving Milliseconds argument to Date(), you must enter a value between 0 to 999 for it.

var myDate = new Date(1995, 1, 23, 14, 25, 30, 999);

myDate.getMilliseconds(); // 999
myDate.myDate.toLocaleString(); // "2/23/1995, 2:25:30 PM"
Amir
  • 1,328
  • 2
  • 13
  • 27
0

Months are zero based in dates, therefore if you pass the number 2, it is treated as march. If you pass a string however it will try to parse it in a way a human would understand it, and dates are usually one based.

As a sidenote, new Date("somestring") was never really specified, and various browsers added support for various formats, therefore you cannot rely on it's behaviour. Date parse is specified and thus reliable.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151