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 Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^