Why Does the Month Argument Range from 0 to 11 in JavaScript's Date Constructor

Why does the month argument range from 0 to 11 in JavaScript's Date constructor?

It's an old (probably unfortunate, probably dying) tradition in the programming world, see the old standard (POSIX) localtime C function http://linux.die.net/man/3/localtime

JavaScript Date month is zero-indexed....but why?

Thats' because it is an old standard from the beginning of computing. You always start counting at 0. But still, I also find this, silly!

Why does this return the number of days in a month?

Basically, the "why" is "the spec told us to". The ECMAScript specification says that the day portion of the values passed to the constructor should be made safe by taking the modulo of the value and 12. It is then passed through some other calculations such that values "wrap" to the preceding or succeeding month.

Passing string values drives these algorithms a little crazy, as answers to Is the Javascript date object always one day off? demonstrate.

In any case, taking care to validate arguments to the Date constructor beforehand before blindly taking what is returned is always a good idea.

Why are my dates aren't working in TypeScript?

Months are zero-indexed, so the month you want to initialize it with is actually 11. It doesn't know what to do with 12, that would be month number 13.

JavaScript Date.new() month offset

Because the specification says so:

Months are identified by an integer in the range 0 to 11, inclusive.

And, even more specifically a little further on:

A month value of 0 specifies January; 1 specifies February; 2 specifies March; 3 specifies April; 4 specifies May; 5 specifies June; 6 specifies July; 7 specifies August; 8 specifies September; 9 specifies October; 10 specifies November; and 11 specifies December.

Why is Month Returning Last Month?

Javascript month begins at 0 not 1

Javascript new date from month and year string

"February 2020" is not a valid input according to the specification thus you should not rely on it to work.

You should convert your input to one that is according to spec and then decide whether you need local time or UTC.

Handling time(zones) is one of the hardest things in JavaScript and I strongly recommend that you do not try to reinvent the wheel here yourself as it is really easy to mess up.
Libraries like momentjs can help you here.

Ambiguous behavior of javascript new Date() constructor for same different formatted string

This is due to new Date("2014-01-01") being created through the Date parse which considers this date to be UTC and then apply the timezone but new Date(["2014","01","01"]) is treated as literal values for each of the parameters in the constructor for your timezone. Although as mentioned in one of the comments below the format new Date(["2014","01","01"]) does not conform to RFC and hence provides different results depending on the browser.

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

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. Support for RFC 2822 format strings is by convention only. Support for ISO 8601 formats differs in that date-only strings (e.g. "1970-01-01") are treated as UTC, not local.

Perhaps what you are after is new Date(Date.UTC(2014,01,01)); Date.UTC in order to create a consistent date that is not bound to the user's configuration.

why does javascript getMonth count from 0 and getDate count from 1?

I assume it's because it would be easier to reference in an array of names, i.e.

var months = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"];

var d = new Date();

var namedMonth = months[d.getMonth()];

If getMonth() returned 1-12, then programmers would have to do d.getMonth()-1 everytime they wanted a fancy named month.

Days of the month don't have specific "names" per se. The getDate() returns 1-(28-31). We usually just refer to them by their number.

The same concept as getMonth() applies for getDay() also, which returns 0-6 based on the day of the week

var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

var namedDay = days[d.getDay()];

All this returns something like:

console.log("Month: month[" + d.getMonth() + "]: " + namedMonth); 
//Month: month[3]: April
console.log("Day: days[" + d.getDay() + "]: " + namedDay);
// Day: days[4] : Thursday


Related Topics



Leave a reply



Submit