Why JavaScript’s Date#getMonth Start From Zero?

Today, one of my colleague asked me a question about JavaScript’s document.lastModified, here was my answer for her:

javascript:dd=new Date(document.lastModified);alert(dd.getFullYear() + “.” + (dd.getMonth() + 1) + “.” + dd.getDate() + “//” + dd.getHours() + “:” + dd.getMinutes() + “:” + dd.getSeconds());

Then she asked me why #getMonth should plus one why others need not. I knew that from the ECMAScript specification, Date#getMonth start from 0 to 11, representing January to December, but I couldn’t answer her question, cause I couldn’t figure out why only #getMonth needs such a hacks which confuse a lot of developers.

After searching a lot on Internet, I got nothing and gave up to found out why. Maybe those specification designers were C or C++ developers who always thought things should start from zero.

This entry was posted in JavaScript, Tricks. Bookmark the permalink.

Comments are closed.