js计算一个月多少天

来源:互联网 发布:php的命名空间 编辑:程序博客网 时间:2024/04/27 17:30

var now = new Date();
var year = now.getFullYear();
var month = now.getMonth()+1;
 var date = now.getDate();


function getDates(ok){


switch(month){
case 4:;
case 6:;
case 9:;
case 11:return 30;break;
case 2:return (year%4==0)&&(year%100!=0||year%400==0)?29:28;break;//判断闰年
default:return 31;
}

}

下面为网上搜的比较简单

//一个月有多少天  
function getDates(){
    var curDate = new Date();
    /* 获取当前月份 */
    var curMonth = curDate.getMonth();
    /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */
    curDate.setMonth(curMonth + 1);
    /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */
    curDate.setDate(0);
    /* 返回当月的天数 */
    return curDate.getDate();
}

0 0
原创粉丝点击