gpt4 book ai didi

Javascript 逐周计算日期

转载 作者:行者123 更新时间:2023-12-02 15:54:38 26 4
gpt4 key购买 nike

大家好,我编写了这段 JavaScript 代码,用于逐步计算一周中任意一天的日期。但是,当代码必须计算月底时,我出错了:例如,在 2015 年 7 月 31 日之后,代码生成 2015 年 7 月 32 日,而不是 2015 年 8 月 1 日。这是我的代码:

var application = this;
var currentDate = new Date();
var stringDate1 = currentDate.getUTCDate() + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate2 = (currentDate.getUTCDate()+1) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate3 = (currentDate.getUTCDate() + 2) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate4 = (currentDate.getUTCDate() + 3) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate5 = (currentDate.getUTCDate() + 4) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate6 = (currentDate.getUTCDate() + 5) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();
var stringDate7 = (currentDate.getUTCDate() + 6) + "/" + (currentDate.getMonth() + 1) + "/" + currentDate.getFullYear();

我的代码哪里错了?有什么帮助吗?BR

最佳答案

只需将小时*分钟*秒*毫秒添加到今天的日期,即可获得明天的日期,依此类推。

var tomorrowsDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);

关于Javascript 逐周计算日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653688/

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