JS:如何计算一个月有多少天

JavaScript012

JS:如何计算一个月有多少天,第1张

function getCountDays() {

        var curDate = new Date()

        /* 获取当前月份 */

        var curMonth = curDate.getMonth()

       /*  生成实际的月份: 由于curMonth会比实际月份小1, 故需加1 */

       curDate.setMonth(curMonth + 1)

       /* 将日期设置为0, 这里为什么要这样设置, 我不知道原因, 这是从网上学来的 */

       curDate.setDate(0)

       /* 返回当月的天数 */

       return curDate.getDate()

}

//例如,  获取当前月份(现在是3月)的总天数: 

getCountDays()       // 返回31

JavaScript获取每个月的天数,参数说明:month:月份year:年,函数被调用后,将直接返回:days天数。

view sourceprint?

01function getDaysInMonth(month,year){

02var days

03if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) days=31

04else if (month==4 || month==6 || month==9 || month==11) days=30

05else if (month==2) {

06if (isLeapYear(year)) { days=29}

07else { days=28}

08}

09return (days)

10}