gpt4 book ai didi

javascript - 通过在今天的日期上添加 6 个月来获取上个月的日期

转载 作者:行者123 更新时间:2023-12-02 16:18:24 28 4
gpt4 key购买 nike

我使用下面的代码将 6 个月添加到今天的日期。

var d = new Date();
var curr_date = d.getDate();
var curr_month = d.getMonth() + 7; //Months are zero based
if(curr_month<10){
curr_month = "0"+curr_month;
}
if(curr_date<10){
curr_date = '0'+curr_date;
}
var curr_year = d.getFullYear();
$scope.vmEndDate = curr_year + "/" + curr_month + "/" + curr_date;

当我打印 $scope.vmEndDate 值时,我得到 2015/09/31 ,但在 9 月 31 日不存在。如何获得正确的值。

最佳答案

您可以直接处理带有月份的日期:

var today = new Date(2015,03,31)
today.setMonth(today.getMonth()+6)
console.log(today.toLocaleDateString())

如果您想获得有效日期,但在九月份,https://stackoverflow.com/a/11469976/4682796

关于javascript - 通过在今天的日期上添加 6 个月来获取上个月的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29367852/

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