why does javascript getMonth count from 0 and getDate count from 1?

前端 未结 3 1775
一整个雨季
一整个雨季 2020-11-28 11:37

This question is purely to satisfy my curiosity.

In the JavaScript Date object, when you call getMonth() it returns the month but it counts from 0.

相关标签:
3条回答
  • 2020-11-28 12:01

    I assume it's because it would be easier to reference in an array of names, i.e.

    var months = ["January", "February", "March", "April", "May", "June", "July",
             "August", "September", "October", "November", "December"];
    
    var d = new Date();
    
    var namedMonth = months[d.getMonth()];
    

    If getMonth() returned 1-12, then programmers would have to do d.getMonth()-1 everytime they wanted a fancy named month.

    Days of the month don't have specific "names" per se. The getDate() returns 1-(28-31). We usually just refer to them by their number.

    The same concept as getMonth() applies for getDay() also, which returns 0-6 based on the day of the week

    var days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    
    var namedDay = days[d.getDay()];
    

    All this returns something like:

    console.log("Month: month[" + d.getMonth() + "]: " + namedMonth); 
    //Month: month[3]:  April
    console.log("Day: days[" + d.getDay() + "]: " + namedDay); 
    // Day: days[4] : Thursday 
    
    0 讨论(0)
  • 2020-11-28 12:04

    Link1

    Date.prototype.getDate()
    Returns the day of the month (1-31) for the specified date according to local time.

    Link2

    A Date object contains a number representing a particular instant in time to within a millisecond For example, if you specify 150 seconds, JavaScript redefines that number as two minutes and 30 seconds.

    When you implement methods in Javascript to find the difference between two times specified in miliseconds, you would need to return a date which needs to be greater than 0 for obvious reasons.

    var startTime = new Date('1/1/1990');  
    var startMsec = startTime.getMilliseconds();  
    startTime.setTime(5000000);  
    var elapsed = (startTime.getTime() - startMsec) / 1000;   
    document.write(elapsed);  
    
    // Output: 5000  
    

    As explained by "SomeShinyObject" that

    var months = ["January", "February", "March", "April", "May", "June", "July",
             "August", "September", "October", "November", "December"];
    

    helps in referencing them through array index.

    Hence getDay, getHours, getMonths starts from 0.

    0 讨论(0)
  • 2020-11-28 12:23

    If you want to say it's inconsistency - you need to ask the creator of specification of language. According to this page JavaScript is based on ECMAScript (EDIT: see @MichaelGeary comment).

    And when you read from page 165 here, you will see that all is working exactly as it's designed.

    For you it can be inconsistency. For me it's rather a feature - 0-based values let you access Array straight away without doing calculations (see @Christopher's answer). In case of day of month you can't really access any Array. It will be weird to have Array of names of days of the month... like this:

    var namesOfDays = [
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", // and again at least 4 times ...
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
        "Sunday", "Monday", "Tuesday"
    ]
    
    0 讨论(0)
提交回复
热议问题