Pages

4/01/2011

Spot a weird bug in Javascript API (Date function)

I have been recently writing a website with extensive javascript on it.
One of the javascript features that I use is Date object and its functions.

Today, I wrote:
var d = new Date();
alert(d.getDate() + "/" + d.getMonth() + "/" + d.getFullYear());

Despite the fact that today is April 1, 2011 (1/4/2011), I got this:
Is my computer's clock wrong?  No, it isn't.

After I checked it in w3school website, it seems like the API of Date is in the weird way.

You can ignore the time related functions which only starts the returning result with 0 (zero) and it makes sense that way.

As you can see the function "getMonth()", it returns result from 0 to 11 ???
I don't understand. Don't people think that January is month number 1 and Decumber is month number 12?

Well, someone could argue that we are programmers and programmers likes to start counting at 0 (zero).  Then, you should take a look at function called "getDate()". Why that function returning result starts with 1 !!!!

I also try "getFullYear()" and it returns ready-to-print result, as same as "getDate()".

So, Why "getMonth()" doesn't?

No comments:

Post a Comment