datetime javascript

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

There are always 12 months in a year, so early C implementations might have used a static fixed-width array with indexes 0,1,2..11.

Let see the example,
//START DATE CONSTRUCTOR
//IF MONTH AND DAY LESS THEN 9, IT SHOULD BE 01, 02,...,09
var zerofill = function(i) {
    return (i < 10 ? '0' : '') + i;
}

var getDateString = function (date) {
    if (date != undefined && date != null) {
        var d = zerofill(date.getDate());
        var m = zerofill(date.getMonth() + 1); ////Month from 0 to 11, so added 1 months
        var y = date.getFullYear();

        return d + "/" + m + "/" + y;
    }
}

var nowDate = new Date();
//CALL DATE STRING with DD/MM/YYYY format
getDateString(nowDate);

//END DATE CONSTRUCTOR
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..

My Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

Why does the month argument range from 0 to 11 in JavaScript Date constructor? Why does the month argument range from 0 to 11 in JavaScript Date constructor? Reviewed by Anil Singh on 10:22 PM Rating: (5)
www.code-sample.com/. Powered by Blogger.
^