猿问

使用JavaScript获取指定月份中的天数?

假设我有月份和数字。



忽然笑
浏览 526回答 3
3回答

芜湖不芜

// Month here is 1-indexed (January is 1, February is 2, etc). This is// because we're using 0 as the day so that it returns the last day// of the last month, so you have to add 1 to the month number // so it returns the correct amount of daysfunction daysInMonth (month, year) {    return new Date(year, month, 0).getDate();}// JulydaysInMonth(7,2009); // 31// FebruarydaysInMonth(2,2009); // 28daysInMonth(2,2008); // 29

慕斯709654

Date.prototype.monthDays= function(){    var d= new Date(this.getFullYear(), this.getMonth()+1, 0);    return d.getDate();}

吃鸡游戏

以下代码获取任何有效的datetime值,并返回关联月份中的天数...消除了其他两个答案的歧义... // pass in any date as parameter anyDateInMonthfunction daysInMonth(anyDateInMonth) {    return new Date(anyDateInMonth.getFullYear(),                     anyDateInMonth.getMonth()+1,                     0).getDate();}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答