gpt4 book ai didi

javascript 添加月份至今

转载 作者:行者123 更新时间:2023-12-02 18:56:40 25 4
gpt4 key购买 nike

我想向给定日期添加 1 个月或 6 个月。但如果我添加一个月,年份不会增加。如果我将 6 月添加到 6 月,我会返回 00 月,但年份会增加。你能帮我一下吗?

function addToBis(monthToAdd){
var tmp = $("#terminbis").val().split('.');

var day = tmp[0];
var month = tmp[1];
var year = tmp[2];

var terminDate = new Date(parseInt(year),parseInt(month), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);

day = "";
month = "";
year = "";

if(terminDate.getDate() < 10){
day = "0"+terminDate.getDate();
} else{
day = terminDate.getDate();
}

if(terminDate.getMonth() < 10){
month = "0"+terminDate.getMonth();
} else{
month = terminDate.getMonth();
}

year = terminDate.getFullYear();


$("#terminbis").val(day+"."+month+"."+year);
}

最佳答案

getMonth 返回 0 到 11 之间的数字,这意味着 0 代表一月,1 代表二月......等等

这样修改

var terminDate = new Date(parseInt(year),parseInt(month - 1), parseInt(day));
terminDate.setMonth(terminDate.getMonth()+monthToAdd);

month = terminDate.getMonth() + 1;

关于javascript 添加月份至今,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15271094/

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