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

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

Appending default dates to array breaks when crossing into new year

问题

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

  1. const handleAddClosureDate = () => {
  2. if (closureDates.length < 5) {
  3. const fromDate = new Date();
  4. const toDate = new Date(fromDate.getTime() + 5 * 24 * 60 * 60 * 1000);
  5. const existingDates = closureDates.map((date) => ({ from: new Date(date.from), to: new Date(date.to) }));
  6. const lastDate = existingDates[existingDates.length - 1];
  7. fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);
  8. toDate.setFullYear(fromDate.getFullYear(), fromDate.getMonth());
  9. const currentYear = fromDate.getFullYear();
  10. fromDate.setFullYear(currentYear);
  11. toDate.setFullYear(currentYear);
  12. setClosureDates((previousDates) => [
  13. ...previousDates,
  14. { from: fromDate.toISOString().substring(0, 10), to: toDate.toISOString().substring(0, 10) },
  15. ]);
  16. }
  17. };
英文:

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:

  1. [
  2. {
  3. &quot;from&quot;: &quot;2023-06-01&quot;,
  4. &quot;to&quot;: &quot;2023-06-08&quot;
  5. },
  6. {
  7. &quot;from&quot;: &quot;2023-07-03&quot;,
  8. &quot;to&quot;: &quot;2023-07-08&quot;
  9. },
  10. {
  11. &quot;from&quot;: &quot;2023-08-03&quot;,
  12. &quot;to&quot;: &quot;2023-08-08&quot;
  13. },
  14. {
  15. &quot;from&quot;: &quot;2023-09-03&quot;,
  16. &quot;to&quot;: &quot;2023-09-08&quot;
  17. },
  18. {
  19. &quot;from&quot;: &quot;2023-10-03&quot;,
  20. &quot;to&quot;: &quot;2023-10-08&quot;
  21. }
  22. ]

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

  1. [
  2. {
  3. &quot;from&quot;: &quot;2023-10-03&quot;,
  4. &quot;to&quot;: &quot;2023-10-08&quot;
  5. },
  6. {
  7. &quot;from&quot;: &quot;2023-11-03&quot;,
  8. &quot;to&quot;: &quot;2023-11-08&quot;
  9. },
  10. {
  11. &quot;from&quot;: &quot;2023-12-03&quot;,
  12. &quot;to&quot;: &quot;2023-12-08&quot;
  13. },
  14. {
  15. &quot;from&quot;: &quot;2024-01-03&quot;,
  16. &quot;to&quot;: &quot;2024-01-08&quot;
  17. },
  18. {
  19. &quot;from&quot;: &quot;2023-02-03&quot;,
  20. &quot;to&quot;: &quot;2023-02-08&quot;
  21. }
  22. ]

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

  1. [
  2. {
  3. &quot;from&quot;: &quot;2024-01-03&quot;,
  4. &quot;to&quot;: &quot;2024-01-08&quot;
  5. },
  6. {
  7. &quot;from&quot;: &quot;2023-02-03&quot;,
  8. &quot;to&quot;: &quot;2023-02-08&quot;
  9. },
  10. {
  11. &quot;from&quot;: &quot;2023-03-03&quot;,
  12. &quot;to&quot;: &quot;2023-03-08&quot;
  13. },
  14. {
  15. &quot;from&quot;: &quot;2023-04-03&quot;,
  16. &quot;to&quot;: &quot;2023-04-08&quot;
  17. },
  18. {
  19. &quot;from&quot;: &quot;2023-05-03&quot;,
  20. &quot;to&quot;: &quot;2023-05-08&quot;
  21. }
  22. ]

How do I fix it? My code is below:

  1. const handleAddClosureDate = () =&gt; {
  2. if (closureDates.length &lt; 5) {
  3. const fromDate = new Date();
  4. const toDate = new Date(fromDate.getTime() + 5 * 24 * 60 * 60 * 1000);
  5. const existingDates = closureDates.map((date) =&gt; ({ from: new Date(date.from), to: new Date(date.to) }));
  6. const lastDate = existingDates[existingDates.length - 1];
  7. fromDate.setMonth(lastDate ? lastDate.from.getMonth() + 1 : fromDate.getMonth() + 1);
  8. toDate.setFullYear(fromDate.getFullYear(), fromDate.getMonth());
  9. const currentYear = fromDate.getFullYear();
  10. fromDate.setFullYear(currentYear);
  11. toDate.setFullYear(currentYear);
  12. setClosureDates((previousDates) =&gt; [
  13. ...previousDates,
  14. { from: fromDate.toISOString().substring(0, 10), to: toDate.toISOString().substring(0, 10) },
  15. ]);
  16. }
  17. };

答案1

得分: 1

在这个代码段中:

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

然后你可以移除这部分:

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

输出:

  1. {from: '2023-10-03', to: '2023-10-08'}
  2. {from: '2023-11-03', to: '2023-11-08'}
  3. {from: '2023-12-03', to: '2023-12-08'}
  4. {from: '2024-01-03', to: '2024-01-08'}
  5. {from: '2024-02-03', to: '2024-02-08'}

在这行代码中:

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

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

  1. const fromDate = new Date();

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

干杯!

英文:

Hm that was a tricky one! <br />

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

Then you can remove this part:

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

Output:

  1. {from: &#39;2023-10-03&#39;, to: &#39;2023-10-08&#39;}
  2. {from: &#39;2023-11-03&#39;, to: &#39;2023-11-08&#39;}
  3. {from: &#39;2023-12-03&#39;, to: &#39;2023-12-08&#39;}
  4. {from: &#39;2024-01-03&#39;, to: &#39;2024-01-08&#39;}
  5. {from: &#39;2024-02-03&#39;, to: &#39;2024-02-08&#39;}

In this line:

  1. 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

  1. 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:

确定