gpt4 book ai didi

javascript - 如何在javascript中获取2,3,4个月的天数

转载 作者:行者123 更新时间:2023-12-02 17:21:05 24 4
gpt4 key购买 nike

如果将 2 , 3 ,4 作为月份数传递给函数,我想获得超过 1 个月的天数,我想获得从今天开始的总天数

function get_days_in_month(months){
days_in_month = new Date();
var y = days_in_month.getFullYear();
var m = days_in_month.getMonth();
if(months==1){
return new Date(y,m+1,0).getDate();
}
if(months==2){
// need to write code or some other logic
}
}

感谢您提前提供帮助

最佳答案

使用 for loop :

var total = 0;

for (i = 1; i <= months; i++) {
total += new Date(y, m + i, 0).getDate();
}

return total;

JSFiddle demo .

关于javascript - 如何在javascript中获取2,3,4个月的天数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23949249/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com