JavaScript 获取指定年份月份的天数
JavaScript 获取指定年份月份的天数
代码:
function getTWO(year, month) {
var days;
if (month === 2) {
if ((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0) {
days = 29;
} else {
days = 28;
} } else if (month === 4 || month === 6 || month === 9 || month === 11) {
days = 30;
} else {
days = 31;
}
return days;
}
function getTHREE(year, month) {
document.write(year + '年' + month + '月有' + getTWO(year, month) + '天');
}
getTHREE(2021, 6);
修改后的代码中,getTWO 函数根据输入的年份和月份,计算该月有多少天,并返回该值。在 getTHREE 函数中,调用 getTWO 函数获取该月的天数,并将结果输出到页面上。同时,将年份和月份一起输出,更加清晰明了。
AI 优化版
原文地址:https://www.cveoy.top/t/topic/mTyk 著作权归作者所有。请勿转载和采集!