为什么使用JavaScript:new Date(year,month,0).getDate()返回当月的天数? [英] Why does JavaScript: new Date(year, month, 0).getDate() return the number of days in the month?
问题描述
我知道这个小小的javascript代码 var what = new Date(year,month,0).getDate()
返回特定年份的特定月份中的天数.但是我似乎不了解背后的逻辑.
I know that this little javascript code,var whatever = new Date(year, month, 0).getDate()
, returns the number of days in a particular month of a particular year. But what I don’t seem to understand is the logic behind it.
在提到年份和月份之后,那个零在那做什么呢?请解释.
What exactly is that zero doing there after we mention the year and the month? Please Explain.
推荐答案
当您给出的参数超出范围时,将调整下一个较大的时间增量以使时间有效.所以:
When you give a parameter out of range, the next larger time increment is adjusted to make the time valid. So:
> new Date(2016,2,1)
2016-03-01T08:00:00.000Z
因此,如果我们指定(2016,2,1),则得到3/1.因此,如果我们指定(2016,2,0),我们将在此日期之前获得一天,并根据需要调整月份以获取有效内容,即上个月的最后一天.
So if we specify (2016,2,1), we get 3/1. So if we specify (2016,2,0), we'll get a day before that, adjusting the month as necessary to get something valid, that is, the last day of the previous month.
> new Date(2016,2,0)
2016-02-29T08:00:00.000Z
这篇关于为什么使用JavaScript:new Date(year,month,0).getDate()返回当月的天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!