Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript is creating date wrong month

Tags:

javascript

using Mozilla Firefox Firebug:

var myDate = new Date(2012, 9, 23, 0,0,0,0); myDate; 

Date {Tue Oct 23 2012 00:00:00 GMT-0400 (Eastern Daylight Time)}

Why does javascript create the date with the wrong month?

like image 911
Joe Kahl Avatar asked Sep 03 '12 21:09

Joe Kahl


People also ask

Why is getMonth 0 based?

This is because the getMonth method returns a zero-based number between 0 and 11 . For example, January is 0 , February is 1 , March is 2 , etc. If you need to get a one-based value for the month, simply add 1 to the result. Copied!

What is default format of date in JavaScript?

The string format should be: YYYY-MM-DDTHH:mm:ss. sssZ , where: YYYY-MM-DD – is the date: year-month-day. The character "T" is used as the delimiter.

How do I get the date back in JavaScript?

Use the setDate() method to get the previous day of a date, e.g. date. setDate(date. getDate() - 1) .

How do I create a specific date in JavaScript?

We can create a date object by passing the Date object a timestamp number in milliseconds. For example, new Date(1572840117245) . When we create a date object by passing it 0 milliseconds, it will return the date object for Jan 01 1970 05:30:00 .


2 Answers

No, javascript's Date months start with 0, so 9 is a 10th month and it is October

Reference:

new Date(year, month [, day, hour, minute, second, millisecond]);

[...]

month Integer value representing the month, beginning with 0 for January to 11 for December.

like image 142
zerkms Avatar answered Sep 20 '22 00:09

zerkms


In the javascript world months begin with zero! kind of weird to me. Anyhow, 9 is NOT September, but rather 9 is October.

like image 44
Joe Kahl Avatar answered Sep 24 '22 00:09

Joe Kahl