为什么 javascript getMonth 从 0 开始计数,getDate 从 1 开始计数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15799514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:08:58  来源:igfitidea点击:

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

javascriptdate

提问by PrimeLens

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.

在 JavaScript Date 对象中,当您调用getMonth()它时会返回月份,但它从 0 开始计数。

0 = January
1 = February 
...

But when you call getDate()it starts counting from 1

但是当你调用getDate()它时,它从 1 开始计数

1 = 1
2 = 2
... 

Why the inconsistency?

为什么不一致?

回答by SomeShinyObject

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()-1everytime they wanted a fancy named month.

如果getMonth()返回1-12,那么程序员d.getMonth()-1每次想要一个花哨的命名月份时都必须这样做。

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.

一个月中的几天本身没有特定的“名称”。的getDate()回报1-(28-31)。我们通常只用他们的号码来称呼他们。

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

同样的概念也getMonth()适用getDay(),它0-6根据星期几返回

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 

回答by Karol

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

如果您想说这是不一致的 - 您需要询问语言规范的创建者。根据此页面JavaScript基于ECMAScript(编辑:请参阅@MichaelGeary 评论)。

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

当您从此处阅读第 165 页时,您会发现一切都按照设计的那样工作。

For you it can be inconsistency. For me it's rather a feature - 0-based values let you access Arraystraight 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 Arrayof names of days of the month... like this:

对你来说,这可能是不一致的。对我来说,它更像是一个功能 - 基于 0 的值让您Array无需进行计算即可直接访问(请参阅@Christopher 的回答)。在一个月的某一天的情况下,您无法真正访问任何Array. 拥有Array一个月中的几天的名称会很奇怪......像这样:

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"
]

回答by Devendra Lattu

Link1

链接1

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

Date.prototype.getDate()
根据本地时间返回指定日期的月份中的第几天 (1-31)。

Link2

链接2

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.

一个 Date 对象包含一个表示特定时间到一毫秒内的数字。例如,如果您指定 150 秒,JavaScript 会将该数字重新定义为两分 30 秒。

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

当你实现JavaScript方法找到毫秒指定的两个时间之间的区别,你就需要返回一个date这就需要将greater than 0显而易见的原因。

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

正如“SomeShinyObject”所解释的那样

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

helps in referencing them through array index.

有助于通过数组索引引用它们。

Hence getDay, getHours, getMonthsstarts from 0.

因此getDay, getHours,getMonths从 0 开始。