如何过滤当月的提醒日期?

How to filter the reminder date for the current month?

我正在从数据库中获取数据,

    recurrence_Date = 2021-10-01,
    recurevery=10, 
    frequency=yearly 
    reminder_setdaysbefore=30

所以开始所有的循环日期应该是2021-09-01,2031-09-01,2041-09-01等等……(从[=49=开始每10年循环一次])

In form, I have two fields from date and to date
a. if the user search 2031-01-01 to 2041-12-01 the result should be 
2031-09-01
2041-09-01
b. if the user search 2031-01-01 to 2031-12-01 the result should be 
2031-09-01

我试过的代码

Date.prototype.addDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate() + days);
  return date;
}

Date.prototype.subtractDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate() - days);
  return date;
}

Date.prototype.addMonths = function(months) {
  var date = new Date(this.valueOf());
  var d = date.getDate();
  date.setMonth(date.getMonth() + months);
  if (date.getDate() != d) {
    date.setDate(0);
  }
  return date;
}

var fromdate_recu_viewedit = "2021-06-01";
var todate_recu_viewedit = "2021-06-31";

const getPaymentPlan = ({
  dateFrom,
  dateTo,
  recurrenceDate,
  daysBefore,
  period,
  skip,
  title
}) => {
  //start from either the recurrence start date, or the start date in the form - whichever is later
  let startDate = (recurrenceDate.getTime() > dateFrom.getTime() ? recurrenceDate : dateFrom);
  //reminders go out several days before the actual recurrence start date
  startDate = startDate.subtractDays(daysBefore);
  let recurrenceList = [];
console.log("dateTo",dateTo)
  while (startDate.getTime() <= dateTo.getTime()) {
  console.log("startDate",startDate.toISOString().split('T')[0])
    recurrenceList.push({
      "project": title,
      "reminderDate": startDate.toISOString().split('T')[0]
    });

    switch (period) {
      case 'Monthly':
        startDate = startDate.addMonths(parseInt(skip));
        break;
      case 'Yearly':
        startDate.setFullYear(startDate.getFullYear() + parseInt(skip));
        break;
      default:
        recurrenceList.push({
          "project": "wrong period type is given",
          "reminderDate": null
        })
        break;
    }
  }

  return recurrenceList;
}


let new_data = {
  "projectremindshow": [{
    "project_ID": "1",
    "project_title": "Althurath",
    "period_type": "Monthly",
    "recurrence_date": "2021-01-01",
    "reminder_set_days": "30",
    "recur_every": "6",
    "start_date": "2020-12-02"
  }, {
    "project_ID": "2",
    "project_title": "Help AG",
    "period_type": "Yearly",
    "recurrence_date": "2021-06-01",
    "reminder_set_days": "30",
    "recur_every": "1",
    "start_date": "2021-05-02"
  }]
}

// console.log(new_data);


let inputList = [];

for (var i = 0; i < new_data.projectremindshow.length; i++) {
  var proj = new_data.projectremindshow[i];

  //add a new entry to inputList for each entry returned from the AJAX call
  inputList.push({
    dateFrom: new Date(fromdate_recu_viewedit),
    dateTo: new Date(todate_recu_viewedit),
    daysBefore: proj.reminder_set_days,
    recurrenceDate: new Date(proj.start_date),
    period: proj.period_type,
    skip: proj.recur_every,
    title: proj.project_title
  });
}

let frequencyList = [];
for (let i = 0; i < inputList.length; i++) {
  let plan = getPaymentPlan(inputList[i]);
  Array.prototype.push.apply(frequencyList, plan);
}
console.log(frequencyList);
frequencyList.sort(compare);
//frequencyList.sort(compare);

$.each(frequencyList, function(index, jsonObject) {
  var tableRow = '<tr>';
  $.each(Object.keys(jsonObject), function(i, key) {
    tableRow += '<td>' + jsonObject[key] + '</td>';
  });
  tableRow += "</tr>";

  $("#tablereminder").last().append(tableRow);




})

function compare(a, b) {
  if (a.reminderDate < b.reminderDate) {

    return -1;
  }
  if (a.reminderDate > b.reminderDate) {
    return 1;
  }
  return 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody id="#tablereminder"></tbody>
</table>

JSON 代码:

{
   "projectremindshow":[
      {
         "project_ID":"1",
         "project_title":"Althurath",
         "period_type":"Monthly",
         "recurrence_date":"2021-01-01",
         "reminder_set_days":"30",
         "recur_every":"6"
      },
      {
         "project_ID":"2",
         "project_title":"Help AG",
         "period_type":"Yearly",
         "recurrence_date":"2021-06-01",
         "reminder_set_days":"30",
         "recur_every":"1"
      }
   ]
}

示例 1
如果用户select, 从日期 ->01-jun-2021 和 今日-> 2021 年 6 月 31 日。 基于 json 我发现了如下的重复列表。但是我得到了错误的日期。

    [
    {
        "project": "Althurath",
        "reminderDate": "2021-05-02"
    },
    {
        "project": "Help AG",
        "reminderDate": "2021-05-02"
    }
]

预计日期如下

 [
        {
            "project": "Althurath",
            "reminderDate": "2021-06-02"
        }
    ]

becoz "start_date":"2020-12-02" 6 个月后提醒日期 =2021-06-02 但我得到的是 2021-05-02。(项目名称 Althurath )

示例 2

如果用户select, 从日期 ->01-01-2021 和 今天-> 31-12-2022。 预计日期如下

     [
            {
                "project": "Althurath",
                "reminderDate": "2021-06-01"
            },
{
                "project": "Althurath",
                "reminderDate": "2021-12-02"
            },
{
                "project": "Althurath",
                "reminderDate": "2022-06-01"
            },,{
                "project": "Help AG",
                "reminderDate": "2022-05-02"
            },
{
                "project": "Althurath",
                "reminderDate": "2022-12-02"
            }
        ]

谁能帮我解决这个问题?

这应该可以满足您的需求。

仅对现有代码进行了一些小改动:

  1. 使用 recurrence_date 而不是 start_date 来填充 inputList
  2. recurrenceList.push 之前添加一个 if 语句以确保它仅在开始日期等于或晚于 dateFrom
  3. 时才将条目添加到列表中
  4. 添加重复日期作为输出对象的额外 属性,只是为了清楚起见

此演示显示了满足示例 2 要求的代码:

Date.prototype.addDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate() + days);
  return date;
}

Date.prototype.subtractDays = function(days) {
  var date = new Date(this.valueOf());
  date.setDate(date.getDate() - days);
  return date;
}

Date.prototype.addMonths = function(months) {
  var date = new Date(this.valueOf());
  var d = date.getDate();
  date.setMonth(date.getMonth() + months);
  if (date.getDate() != d) {
    date.setDate(0);
  }
  return date;
}

var fromdate_recu_viewedit = "2021-01-01";
var todate_recu_viewedit = "2022-12-31";

const getPaymentPlan = ({
  dateFrom,
  dateTo,
  recurrenceDate,
  daysBefore,
  period,
  skip,
  title
}) => {
  //calculate the reminder date. reminders go out several days before the actual recurrence start date
  startDate = recurrenceDate.subtractDays(daysBefore);
  let recurrenceList = [];
  
  while (startDate.getTime() <= dateTo.getTime()) {
    var recurDate = startDate.addDays(daysBefore); //just adding the recurrence Date to the output object, for clarity
    
    //only add the item to the list if the reminder date is later than dateFrom
    if (startDate.getTime() >= dateFrom.getTime()) recurrenceList.push({
      "project": title,
      "reminderDate": startDate.toISOString().split('T')[0],
      "recurrenceDate": recurDate.toISOString().split('T')[0]
    });

    switch (period) {
      case 'Monthly':
        startDate = startDate.addMonths(parseInt(skip));
        break;
      case 'Yearly':
        startDate.setFullYear(startDate.getFullYear() + parseInt(skip));
        break;
      default:
        recurrenceList.push({
          "project": "wrong period type is given",
          "reminderDate": null
        })
        break;
    }
  }

  return recurrenceList;
}


let new_data = {
   "projectremindshow":[
      {
         "project_ID":"1",
         "project_title":"Althurath",
         "period_type":"Monthly",
         "recurrence_date":"2021-01-01",
         "reminder_set_days":"30",
         "recur_every":"6"
      },
      {
         "project_ID":"2",
         "project_title":"Help AG",
         "period_type":"Yearly",
         "recurrence_date":"2021-06-01",
         "reminder_set_days":"30",
         "recur_every":"1"
      }
   ]
}

// console.log(new_data);


let inputList = [];

for (var i = 0; i < new_data.projectremindshow.length; i++) {
  var proj = new_data.projectremindshow[i];

  //add a new entry to inputList for each entry returned from the AJAX call
  inputList.push({
    dateFrom: new Date(fromdate_recu_viewedit),
    dateTo: new Date(todate_recu_viewedit),
    daysBefore: parseInt(proj.reminder_set_days),
    recurrenceDate: new Date(proj.recurrence_date),
    period: proj.period_type,
    skip: proj.recur_every,
    title: proj.project_title
  });
}

let frequencyList = [];
for (let i = 0; i < inputList.length; i++) {
  let plan = getPaymentPlan(inputList[i]);
  Array.prototype.push.apply(frequencyList, plan);
}
console.log(frequencyList);
frequencyList.sort(compare);
//frequencyList.sort(compare);

$.each(frequencyList, function(index, jsonObject) {
  var tableRow = '<tr>';
  $.each(Object.keys(jsonObject), function(i, key) {
    tableRow += '<td>' + jsonObject[key] + '</td>';
  });
  tableRow += "</tr>";

  $("#tablereminder").last().append(tableRow);




})

function compare(a, b) {
  if (a.reminderDate < b.reminderDate) {

    return -1;
  }
  if (a.reminderDate > b.reminderDate) {
    return 1;
  }
  return 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tbody id="#tablereminder"></tbody>
</table>