JavaScript获取每个月的天数

1.getDate()

function getMonthDay(year, month) {
   let days = new Date(year, month, 0).getDate()
   return days
}
getMonthDay(2021, 3)//30

2.setDate()

function getMonthDay(year, month) {
   let days = new Date(year, month, 0).setDate()
   return days
}
getMonthDay(2021, 3)//30

3.闰年平年判断法

每一年里,只有2月是不固定的。1、3、5、7、8、10、12是31天,4、6、8、11是30天写死,2月判断润平年。能被4整除且不能整除100的为闰年或者能够被 400 整除的就是闰年。

function getMonthDay(year) {
   if(year%4==0&&year%100!=0||year%400==0)return 29
   return 28
} 
getMonthDay(
2021)//28
getMonthDay(2020)//29

 

posted @ 2021-09-16 14:01  肥晨  阅读(887)  评论(0编辑  收藏  举报