将默认日期添加到数组中在跨入新年时会出现问题。

huangapple go评论63阅读模式
英文:

Appending default dates to array breaks when crossing into new year

问题

以下是您要翻译的代码部分:

  const handleAddClosureDate = () => {
    if (closureDates.length < 5) {
      const fromDate = new Date();
      const toDate = new Date(fromDate.getTime() + 5 * 24 * 60 * 60 * 1000);

      const existingDates = closureDates.map((date) => ({ from: new Date(date.from), to: new Date(date.to) }));
      const lastDate = existingDates[existingDates.length - 1];

      fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);
      toDate.setFullYear(fromDate.getFullYear(), fromDate.getMonth());

      const currentYear = fromDate.getFullYear();
      fromDate.setFullYear(currentYear);
      toDate.setFullYear(currentYear);

      setClosureDates((previousDates) => [
        ...previousDates,
        { from: fromDate.toISOString().substring(0, 10), to: toDate.toISOString().substring(0, 10) },
      ]);
    }
  };
英文:

I am working on a form component which creates a list of scheduled closure dates for retailers.

At the moment, I am having some difficulty with anything that crosses into the new year when setting default values.

For example, from a recent run here is my output:

Add initial set of closure dates:

[
    {
        &quot;from&quot;: &quot;2023-06-01&quot;,
        &quot;to&quot;: &quot;2023-06-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-07-03&quot;,
        &quot;to&quot;: &quot;2023-07-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-08-03&quot;,
        &quot;to&quot;: &quot;2023-08-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-09-03&quot;,
        &quot;to&quot;: &quot;2023-09-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-10-03&quot;,
        &quot;to&quot;: &quot;2023-10-08&quot;
    }
]

Remove all but last added closure date, then add 4 more new closure dates:

[
    {
        &quot;from&quot;: &quot;2023-10-03&quot;,
        &quot;to&quot;: &quot;2023-10-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-11-03&quot;,
        &quot;to&quot;: &quot;2023-11-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-12-03&quot;,
        &quot;to&quot;: &quot;2023-12-08&quot;
    },
    {
        &quot;from&quot;: &quot;2024-01-03&quot;,
        &quot;to&quot;: &quot;2024-01-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-02-03&quot;,
        &quot;to&quot;: &quot;2023-02-08&quot;
    }
]

Remove all but last 2 entries and add more closure dates:

[
    {
        &quot;from&quot;: &quot;2024-01-03&quot;,
        &quot;to&quot;: &quot;2024-01-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-02-03&quot;,
        &quot;to&quot;: &quot;2023-02-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-03-03&quot;,
        &quot;to&quot;: &quot;2023-03-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-04-03&quot;,
        &quot;to&quot;: &quot;2023-04-08&quot;
    },
    {
        &quot;from&quot;: &quot;2023-05-03&quot;,
        &quot;to&quot;: &quot;2023-05-08&quot;
    }
]

How do I fix it? My code is below:

  const handleAddClosureDate = () =&gt; {
    if (closureDates.length &lt; 5) {
      const fromDate = new Date();
      const toDate = new Date(fromDate.getTime() + 5 * 24 * 60 * 60 * 1000);

      const existingDates = closureDates.map((date) =&gt; ({ from: new Date(date.from), to: new Date(date.to) }));
      const lastDate = existingDates[existingDates.length - 1];

      fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);
      toDate.setFullYear(fromDate.getFullYear(), fromDate.getMonth());

      const currentYear = fromDate.getFullYear();
      fromDate.setFullYear(currentYear);
      toDate.setFullYear(currentYear);

      setClosureDates((previousDates) =&gt; [
        ...previousDates,
        { from: fromDate.toISOString().substring(0, 10), to: toDate.toISOString().substring(0, 10) },
      ]);
    }
  };

答案1

得分: 1

在这个代码段中:

fromDate.setFullYear(lastDate ? lastDate.from.getFullYear() : fromDate.getFullYear()); // <-- Add this line
fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);

然后你可以移除这部分:

const currentYear = fromDate.getFullYear();
// fromDate.setFullYear(currentYear); // <- Remove
toDate.setFullYear(currentYear);

输出:

{from: '2023-10-03', to: '2023-10-08'}
{from: '2023-11-03', to: '2023-11-08'}
{from: '2023-12-03', to: '2023-12-08'}
{from: '2024-01-03', to: '2024-01-08'}
{from: '2024-02-03', to: '2024-02-08'}

在这行代码中:

fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);

如果lastDate是在12月份,getMonth() + 1会自动将1月份设置为下一年。但是因为

const fromDate = new Date();

获取了当前年份,你需要根据lastDate的先前值来设置fullYear,类似于你在月份上所做的操作。

干杯!

英文:

Hm that was a tricky one! <br />

fromDate.setFullYear(lastDate ? lastDate.from.getFullYear() : fromDate.getFullYear()); // &lt;-- Add this line
fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);

Then you can remove this part:

const currentYear = fromDate.getFullYear();
// fromDate.setFullYear(currentYear); // &lt;- Remove
toDate.setFullYear(currentYear);

Output:

{from: &#39;2023-10-03&#39;, to: &#39;2023-10-08&#39;}
{from: &#39;2023-11-03&#39;, to: &#39;2023-11-08&#39;}
{from: &#39;2023-12-03&#39;, to: &#39;2023-12-08&#39;}
{from: &#39;2024-01-03&#39;, to: &#39;2024-01-08&#39;}
{from: &#39;2024-02-03&#39;, to: &#39;2024-02-08&#39;}

In this line:

fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);

If lastDate was on december, the getMonth() + 1 automatically sets the January to the next year. <br />
But because

const fromDate = new Date();

gets the current year, you need to set the fullYear based on the previous value from lastDate, similar to what you did with the months. <br />
Cheers!

huangapple
  • 本文由 发表于 2023年7月3日 22:36:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76605764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定