js根据年和月获取该月有几天:
/**
* 判断某年是否闰年
*/
export function isRuinian(year){
if(year/4 == 0 && year/100 != 0){
return 29;
} else if (year/400 == 0){
return 29;
} else{
return 28;
}
};
/**
* 根据年和月获取该月有几天
*/
export function getDayNumByYearMonth(year,month){
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
case 2:
return isRuinian(year);
}
};
先用export导出这个函数
然后在引用的页面用import导入这个函数:
import { getDayNumByYearMonth } from "../../assets/js/api/config";
本文介绍了一个实用的JavaScript函数,用于判断闰年并根据年份和月份获取该月的天数。通过简单的switch-case语句和闰年判断逻辑,开发者可以轻松获取指定月份的天数,适用于日历应用或日期相关的项目。
720

被折叠的 条评论
为什么被折叠?



