Skip to main content

Posts

Showing posts with the label JavaScript Date constructor

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 DAT...