获取某个月最后一天(总天数)

方法一:自己根据年份判断是否闰年,再根据月份得出天数

export const getDaysInMonth = (year, month) => {
    let isLeapYear = year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
    if (parseInt(month, 10) === 2) {
        return isLeapYear ? 29 : 28;
    }
    return Math.ceil(Math.abs(month - 7.5)) % 2 + 30;
}

方法二:利用原生new Date()方法获取


export const getDaysInMonth =(year, month) =>{ 
  let d
= new Date(year,month,0);
  return d.getDate();
}

 

posted @ 2020-12-24 10:10  小情绪^_^  阅读(239)  评论(0编辑  收藏  举报