为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

忽然笑 2019-11-23 11:11:31
假设我有月份和数字。
查看完整描述

3 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

// 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 days

function daysInMonth (month, year) {

    return new Date(year, month, 0).getDate();

}


// July

daysInMonth(7,2009); // 31

// February

daysInMonth(2,2009); // 28

daysInMonth(2,2008); // 29


查看完整回答
反对 回复 2019-11-23
?
慕斯709654

TA贡献1840条经验 获得超5个赞

Date.prototype.monthDays= function(){

    var d= new Date(this.getFullYear(), this.getMonth()+1, 0);

    return d.getDate();

}


查看完整回答
反对 回复 2019-11-23
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

以下代码获取任何有效的datetime值,并返回关联月份中的天数...消除了其他两个答案的歧义...


 // pass in any date as parameter anyDateInMonth

function daysInMonth(anyDateInMonth) {

    return new Date(anyDateInMonth.getFullYear(), 

                    anyDateInMonth.getMonth()+1, 

                    0).getDate();}


查看完整回答
反对 回复 2019-11-23
  • 3 回答
  • 0 关注
  • 524 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号