Alex Rivera | Logout

Javascript Date method inconsistency - getDate vs getMonth

Asked 2012-03-13T15:56:02.483
15

This caused me a bit of a headache last night and I wanted to understand why the getDate method in the Date object is 1 based (returns values from 1-31) while the getMonth method is 0 based (returns 0-11). I'm wondering why there is this inconsistency in methods for the same object.

I understand why it's difficult to change the behavior now but are there any reasons this was designed like this in the first place?

Documentation can be found here: http://www.w3schools.com/jsref/jsref_obj_date.asp

Edit
Report

1 Answer

6

Hard to tell, but I have a suspicion that Month is 0-based so it can be easily used as an indexer in a months array that holds month names

var months = new ["Jan", "Feb", ....]
months[new Date().getMonth()];
answered 2012-03-13T15:59:32.040

Your Answer