首页 > 技术文章 > 获取某个月最后一天(总天数)

qilj 2020-12-24 10:10 原文

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

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();
}

 

推荐阅读