How To Filter The Reminder Date For The Current Month?
I am fetching data from database are, recurrence_Date = 2021-10-01, recurevery=10, frequency=yearly reminder_setdaysbefore=30 so the start all the recurrence dat
Solution 1:
This should do what you need.
Just a couple of minor changes from the existing code:
- Use recurrence_date instead of start_date to populate the inputList
- Add an
if
statement beforerecurrenceList.push
to ensure it only adds the entry to the list if the startDate is on or afterdateFrom
- Added the recurrence date as an extra property of the output object, just for clarity
This demo shows the code meeting the requirements for your Example 2:
Date.prototype.addDays = function(days) {
var date = newDate(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
Date.prototype.subtractDays = function(days) {
var date = newDate(this.valueOf());
date.setDate(date.getDate() - days);
return date;
}
Date.prototype.addMonths = function(months) {
var date = newDate(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";
constgetPaymentPlan = ({
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 dateFromif (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: newDate(fromdate_recu_viewedit),
dateTo: newDate(todate_recu_viewedit),
daysBefore: parseInt(proj.reminder_set_days),
recurrenceDate: newDate(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);
})
functioncompare(a, b) {
if (a.reminderDate < b.reminderDate) {
return -1;
}
if (a.reminderDate > b.reminderDate) {
return1;
}
return0;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><table><tbodyid="#tablereminder"></tbody></table>
Post a Comment for "How To Filter The Reminder Date For The Current Month?"