简体   繁体   English

NodeJS 日期加月创建无限循环

[英]NodeJS date adding month creates infinite loop

I am trying to make a cloud function in firebase that does something every day, week, month or year for max.我正在尝试在 firebase 中创建一个云 function,它每天、每周、每月或每年都做一些事情,最大。 1 year into the future.未来 1 年。 Somehow the date looping doesnt work.不知何故,日期循环不起作用。 Does anyone know why this is?有人知道为什么吗? My Code:我的代码:

//Format date to yyyy-mm-dd
  const splitDate = date.split('-');
  const year = splitDate[2];
  const month = splitDate[1];
  const day = splitDate[0];

  const nextYear = [parseInt(year) + 1, parseInt(month)  - 1, parseInt(day)];

  let curDate = new Date(year, parseInt(month) - 1, day);

  while(true){
    //increase the date of the appointment by the specified amount of time each iteration
    if (recurring === 'daily') {
        curDate.setDate(curDate.getDate() + 1);
    } else if (recurring === 'weekly') {
        curDate.setDate(curDate.getDate() + 7);
    } else if (recurring === 'monthly') {
        curDate.setMonth(curDate.getMonth() + 1);
    } else if(recurring === 'yearly') {
        curDate.setFullYear(curDate.getFullYear() + 1);
    } else{
        console.log('recurring value was not entered or invalid, function aborted');
      break;
    }
    console.log(curDate);

    //Test if end date is passed. if so, stop function
    if (nextYear[0] < curDate.getFullYear()) {
      break;
    } else if (nextYear[0] == curDate.getFullYear()) {
        if (nextYear[1] < curDate.getMonth() + 1) {
            break;
        } else if (nextYear[1] == curDate.getMonth()) {
            if (nextYear[2] < curDate.getDate()) {
              break;
            }
        }
    }

    //format new data, add a field that indicates this appointment does not need a creation email

    //Add the appointment with the new data in the appointments collection
  }
  return true;
})

This code almost works, but it doesnt get exactly the right date.这段代码几乎可以工作,但它没有得到完全正确的日期。 It stops at the following when testing daily.每天测试时停在以下位置。

2021-09-01T00:00:00.000Z 

It also misses a week when trying weekly.每周尝试时也会错过一周。 I think it is because of the date checking not being correct.我认为这是因为日期检查不正确。 The monthly and yearly do work and weekly and daily end on the correct month.每月和每年工作,每周和每天在正确的月份结束。 Does anyone know why it instantly does break when it hits the right month in the right year?有谁知道为什么它在正确的年份遇到正确的月份时会立即中断?

There are several issues that I've noticed within your code:我在您的代码中注意到了几个问题:

1 - When testing your code it didn't seem that the date was being calculated correctly. 1 - 在测试您的代码时,似乎没有正确计算日期。 I have modified it the following way in order to fix that:为了解决这个问题,我按照以下方式对其进行了修改:

let curDate = new Date(year, month, day);

while(true){
    //increase the date of the appointment by the specified amount of time each iteration
    if (recurring === 'daily') {
        curDate.setDate(curDate.getDate() + 7);
    } else if (recurring === 'weekly') {
        curDate.setDate(curDate.getDate() + 7);
    } else if (recurring === 'monthly') {
        curDate.setMonth(curDate.getMonth() + 7);
    } else if(recurring === 'yearly') {
        curDate.setFullYear(curDate.getFullYear() + 7);
    } else{
        console.log('recurring value was not entered or invalid, function aborted');
      break;
    }
    console.log(curDate);
...

Note, that I have removed the formatDate using the individual date and time component values instead, as passing an array to new Date() was giving me a wrong month (it was reading 8 as August, while in JS is should represent September since months start from 0) .请注意,我已经使用单独的日期和时间组件删除了formatDate ,因为将数组传递给new Date()给了我一个错误的月份(它读为 8 月,而在 JS 中应该代表九月,因为几个月从 0) 开始


2 - The getYear() method is deprecated, you should use the getFullYear() instead. 2 - getYear()方法已弃用,您应该改用getFullYear()


3 - You should change the less-than signs to the greater-than signs for the day and month in your last If-Else statement within the loop, so it looks like this: 3 - 您应该将循环中最后一个 If-Else 语句中日期和月份的小于号更改为大于号,因此它看起来像这样:

----------

EDIT编辑

Looking further into this issue, it seems that your last If-Else statement was actually correct initially.进一步研究这个问题,您的最后一个 If-Else 语句似乎最初是正确的。 Bringing back the less-than signs should resolve the new issue for you:带回小于号应该可以为您解决新问题:

if (nextYear[0] < curDate.getFullYear()) {
  break;
} else if (nextYear[0] == curDate.getFullYear()) {
    if (nextYear[1] < curDate.getMonth()) {
        break;
    } else if (nextYear[1] == curDate.getMonth()) {
        if (nextYear[2] < curDate.getDate()) {
          break;
        }
    }
}

Let me know if it helps.让我知道是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM