日期未正确递增

发布于 2025-01-11 18:36:52 字数 1053 浏览 0 评论 0原文

我有一个函数,当调用时,它会在输入的日期上添加 7 天。日期的格式为 YYYY-MM-DD (即 2022-03-02)

for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    //console.log("Old Date: ");
    //console.log(temp.toISOString().split('T')[0])
    //console.log(temp.getDate());
    temp.setDate(temp.getDate() + 7);

    console.log("New Date: ");
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

由于某种原因,当该函数作为重复(涉及重复事件的 React Web 应用程序)的一部分被调用时, addWeek 函数将无法正确递增一次,然后在其余时间正确递增。

以下是我将 RepeatCount 设置为 5 时的最新日志的输入:

Old Date:
2022-03-04
New Date:
2022-03-11

Old Date:
2022-03-11
New Date:
2022-03-17

Old Date:
2022-03-17
New Date:
2022-03-24

Old Date:
2022-03-24
New Date:
2022-03-31

Old Date:
2022-03-31
New Date:
2022-04-07

正如您可能已经注意到的,除了第二次重复之外,它会正确增加周数。我已经用不同的日期对此进行了多次测试,每次都只有第二次迭代被错误地增加。其他一切都很好。

请帮忙。我对此失去了理智。

我忘了之前添加:addWeek 将日期作为字符串输入。

I have a function that when called, it will add 7 days to the inputted date. The format for the date is YYYY-MM-DD (i.e. 2022-03-02)

for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    //console.log("Old Date: ");
    //console.log(temp.toISOString().split('T')[0])
    //console.log(temp.getDate());
    temp.setDate(temp.getDate() + 7);

    console.log("New Date: ");
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

For some reason, when the function is called as part of a repeat (React Web Application that involves recurring events), the addWeek function will not increment correctly a single time but then increment correctly the rest of the time.

Here's the input from my most recent log when I set repeatCount to 5:

Old Date:
2022-03-04
New Date:
2022-03-11

Old Date:
2022-03-11
New Date:
2022-03-17

Old Date:
2022-03-17
New Date:
2022-03-24

Old Date:
2022-03-24
New Date:
2022-03-31

Old Date:
2022-03-31
New Date:
2022-04-07

As you've probably noticed, it increments the week correctly with the exception of the second repeat. I've tested this multiple times with different dates, and each time, it is only the second iteration that is incremented incorrectly. Everything else works fine.

Please help. I'm losing my mind over this.

I forgot to add earlier: addWeek takes the date as a string input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光瘦了 2025-01-18 18:36:52

这是夏令时问题。 JavaScript 中的日期本质上是本地的,因此当您使用 setDate() 时,它会尝试在新日期上保持同一时间,从而考虑到夏令时的变化。但这意味着与 UTC 时间(toISOString() 转换为 UTC 时间)相比,一天中的时间会有所不同。示例中第二个输出上的 temp 实际值是 2022-03-17T23:00Z,比您要查找的日期早一小时。但你的代码去掉了时间元素,所以你最终休息了一天。

不要使用 setDate(),而是使用 Date 构造函数:

    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

var date = new Date('2022-03-02');
const repeatCount = 5;
for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

    console.log("New Date: ", temp);
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

This is a Daylight Savings Time problem. Dates in JavaScript are inherently local, so when you use setDate() it tries to keep the same time of day on the new date, accounting for shifts in Daylight Savings Time. But that means the time of day will be different when compared to UTC time (which toISOString() converts to). The actual value of temp on the second output in your example is 2022-03-17T23:00Z, one hour before the date you were looking for. But your code strips off the time element so you end up one day off instead.

Instead of using setDate(), use the Date constructor:

    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

var date = new Date('2022-03-02');
const repeatCount = 5;
for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

    console.log("New Date: ", temp);
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

灼痛 2025-01-18 18:36:52

https://jsfiddle.net/4wm1vz9d/1/

function addWeek(date)
{
    date.setDate(date.getDate()+7)
}

let date = new Date();
console.log(date)
for(let i = 0; i < 5; i++)
{
    addWeek(date)
    console.log(date)
}

https://jsfiddle.net/4wm1vz9d/1/

function addWeek(date)
{
    date.setDate(date.getDate()+7)
}

let date = new Date();
console.log(date)
for(let i = 0; i < 5; i++)
{
    addWeek(date)
    console.log(date)
}
不再让梦枯萎 2025-01-18 18:36:52

唷。 JavaScript 有时很奇怪。由于转换为 ISO 字符串,日期获取似乎不准确。尝试返回临时日期对象而不是字符串。然后添加周后进行转换。它可能与对象提供的其他值有关......

Phew. Javascript is sometimes weird. Seems like the date get's inaccurate because of the conversion to a ISO String. Try returning the temp Date object instead of the string. Then convert it after adding the weeks. It may have something to do with the other values which the Object provides...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文