如何过滤当月的提醒日期? [英] How to filter the reminder date for the current month?
问题描述
我正在从数据库中获取数据,
Recurrent_Date = 2021-10-01,每次复发=10,频率=每年alert_setdaysbefore=30
所以开始所有的重复日期应该是2021-09-01,2031-09-01,2041-09-01等等......(从start_date开始每10年重复一次)
在表单中,我有两个字段,从日期到日期一种.如果用户搜索 2031-01-01 到 2041-12-01 结果应该是2031-09-012041-09-01湾如果用户搜索 2031-01-01 到 2031-12-01 结果应该是2031-09-01
我尝试过的代码
Date.prototype.addDays = function(days) {var date = new Date(this.valueOf());date.setDate(date.getDate() + 天数);归期;}Date.prototype.subtractDays = 函数(天){var date = new Date(this.valueOf());date.setDate(date.getDate() - 天);归期;}Date.prototype.addMonths = 函数(月){var date = new Date(this.valueOf());var d = date.getDate();date.setMonth(date.getMonth() + 月);如果(日期.getDate()!= d){日期.setDate(0);}归期;}var fromdate_recu_viewedit = "2021-06-01";var todate_recu_viewedit = "2021-06-31";const getPaymentPlan = ({日期从,日期到,复发日期,几天前,时期,跳过,标题}) =>{//从重复开始日期或表单中的开始日期开始 - 以较晚者为准让 startDate = (recurrenceDate.getTime() > dateFrom.getTime() ? RecurrenceDate : dateFrom);//提醒在实际重复开始日期前几天发出startDate = startDate.subtractDays(daysBefore);让regressionList = [];console.log("dateTo",dateTo)while (startDate.getTime() <= dateTo.getTime()) {console.log("startDate",startDate.toISOString().split('T')[0])重复列表.push({项目名称,提醒日期":startDate.toISOString().split('T')[0]});开关(句号){案例每月":startDate = startDate.addMonths(parseInt(skip));休息;案例每年":startDate.setFullYear(startDate.getFullYear() + parseInt(skip));休息;默认:重复列表.push({"project": "给出了错误的期间类型",提醒日期":空})休息;}}返回重复列表;}让新数据 = {projectremindshow":[{"project_ID": "1","project_title": "阿尔苏拉斯","period_type": "每月","recurrence_date": "2021-01-01","reminder_set_days": "30","recur_every": "6",开始日期":2020-12-02"}, {"project_ID": "2","project_title": "帮助 AG","period_type": "每年","recurrence_date": "2021-06-01","reminder_set_days": "30","recur_every": "1",开始日期":2021-05-02"}]}//console.log(new_data);让 inputList = [];for (var i = 0; i < new_data.projectremindshow.length; i++) {var proj = new_data.projectremindshow[i];//为从 AJAX 调用返回的每个条目向 inputList 添加一个新条目inputList.push({dateFrom: 新日期(fromdate_recu_viewedit),dateTo:新日期(todate_recu_viewedit),daysBefore: proj.reminder_set_days,复发日期:新日期(proj.start_date),期间:proj.period_type,跳过:proj.recur_every,标题:proj.project_title});}让频率列表 = [];for (let i = 0; i < inputList.length; i++) {让计划 = getPaymentPlan(inputList[i]);Array.prototype.push.apply(frequencyList, plan);}控制台日志(频率列表);频率列表.排序(比较);//频率列表.排序(比较);$.each(frequencyList, function(index, jsonObject) {var tableRow = '';$.each(Object.keys(jsonObject), function(i, key) {tableRow += ''+ jsonObject[key] + '</td>';});tableRow += "</tr>";$("#tablereminder").last().append(tableRow);})函数比较(a,b){if (a.reminderDate < b.reminderDate) {返回-1;}if (a.reminderDate > b.reminderDate) {返回 1;}返回0;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><表格><tbody id="#tablereminder"></tbody></table>
JSON 代码:
<代码>{项目提醒显示":[{project_ID":1",project_title":Althurath",period_type":每月",recurrence_date":2021-01-01",reminder_set_days":30",recur_every":6"},{project_ID":2",project_title":Help AG",period_type":每年",recurrence_date":2021-06-01",reminder_set_days":30",recur_every":1"}]}
示例 1
如果用户选择,从日期 -> 01-june-2021 和迄今为止->2021 年 6 月 31 日.基于json,我找到了如下的重复列表.但是这里的日期有误.
<预><代码> [{项目":阿尔苏拉斯",提醒日期":2021-05-02"},{项目":帮助AG",提醒日期":2021-05-02"}]预计日期如下
<预><代码> [{项目":阿尔苏拉斯",提醒日期":2021-06-02"}]becoz开始日期":2020-12-02"6 个月后,将是提醒日期 =2021-06-02,但现在是 2021-05-02.(对于项目名称 Althurath)
示例 2
如果用户选择,从日期 -> 01-01-2021 和迄今为止->31-12-2022.预计日期如下
<预><代码> [{项目":阿尔苏拉斯",提醒日期":2021-06-01"},{项目":阿尔苏拉斯",提醒日期":2021-12-02"},{项目":阿尔苏拉斯",提醒日期":2022-06-01"},,{项目":帮助AG",提醒日期":2022-05-02"},{项目":阿尔苏拉斯",提醒日期":2022-12-02"}]谁能帮我解决这个问题?
解决方案 这应该可以满足您的需求.
仅对现有代码稍作改动:
- 使用regression_date 而不是start_date 来填充inputList
- 在
recurrenceList.push
之前添加一个 if
语句以确保它只在 startDate 是在 dateFrom
上或之后将条目添加到列表中 - 为了清晰起见,添加了重复日期作为输出对象的额外属性
此演示展示了满足示例 2 要求的代码:
Date.prototype.addDays = function(days) {var date = new Date(this.valueOf());date.setDate(date.getDate() + 天数);归期;}Date.prototype.subtractDays = 函数(天){var date = new Date(this.valueOf());date.setDate(date.getDate() - 天);归期;}Date.prototype.addMonths = 函数(月){var date = new Date(this.valueOf());var d = date.getDate();date.setMonth(date.getMonth() + 月);如果(日期.getDate()!= d){日期.setDate(0);}归期;}var fromdate_recu_viewedit = "2021-01-01";var todate_recu_viewedit = "2022-12-31";const getPaymentPlan = ({日期从,日期到,复发日期,几天前,时期,跳过,标题}) =>{//计算提醒日期.提醒在实际重复开始日期前几天发出startDate = RecurrenceDate.subtractDays(daysBefore);让regressionList = [];while (startDate.getTime() <= dateTo.getTime()) {var recurDate = startDate.addDays(daysBefore);//为了清楚起见,只是将重复日期添加到输出对象中//只有在提醒日期晚于dateFrom时才将项目添加到列表中if (startDate.getTime() >= dateFrom.getTime()) RecurrentList.push({项目名称,"reminderDate": startDate.toISOString().split('T')[0],"recurrenceDate": recurDate.toISOString().split('T')[0]});开关(句号){案例每月":startDate = startDate.addMonths(parseInt(skip));休息;案例每年":startDate.setFullYear(startDate.getFullYear() + parseInt(skip));休息;默认:重复列表.push({"project": "给出了错误的期间类型",提醒日期":空})休息;}}返回重复列表;}让新数据 = {projectremindshow":[{"project_ID":"1","project_title":"阿尔苏拉斯","period_type":"每月","recurrence_date":"2021-01-01","reminder_set_days":"30","recur_every":"6"},{"project_ID":"2","project_title":"帮助AG","period_type":"每年","recurrence_date":"2021-06-01","reminder_set_days":"30","recur_every":"1"}]}//console.log(new_data);让 inputList = [];for (var i = 0; i < new_data.projectremindshow.length; i++) {var proj = new_data.projectremindshow[i];//为从 AJAX 调用返回的每个条目向 inputList 添加一个新条目inputList.push({dateFrom: 新日期(fromdate_recu_viewedit),dateTo:新日期(todate_recu_viewedit),daysBefore: parseInt(proj.reminder_set_days),复发日期:新日期(proj.recurrence_date),期间:proj.period_type,跳过:proj.recur_every,标题:proj.project_title});}让频率列表 = [];for (let i = 0; i < inputList.length; i++) {让计划 = getPaymentPlan(inputList[i]);Array.prototype.push.apply(frequencyList, plan);}控制台日志(频率列表);频率列表.排序(比较);//频率列表.排序(比较);$.each(frequencyList, function(index, jsonObject) {var tableRow = '';$.each(Object.keys(jsonObject), function(i, key) {tableRow += ''+ jsonObject[key] + '</td>';});tableRow += "</tr>";$("#tablereminder").last().append(tableRow);})函数比较(a,b){if (a.reminderDate < b.reminderDate) {返回-1;}if (a.reminderDate > b.reminderDate) {返回 1;}返回0;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><表格><tbody id="#tablereminder"></tbody></table>
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 date should be 2021-09-01,2031-09-01,2041-09-01 so on......(recur every 10 years from the start_date)
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
CODE I TRIED
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 CODE:
{
"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"
}
]
}
Example 1
If user select,
from date ->01-june-2021 and
todate-> 31-Jun-2021.
Based on json i found recurrence list like below. But Here am getting wrong dates.
[
{
"project": "Althurath",
"reminderDate": "2021-05-02"
},
{
"project": "Help AG",
"reminderDate": "2021-05-02"
}
]
expected dates are like below
[
{
"project": "Althurath",
"reminderDate": "2021-06-02"
}
]
becoz "start_date":"2020-12-02" and after 6 months it will be reminderDate =2021-06-02 but am getting 2021-05-02.(for the project name Althurath)
Example2
If user select,
from date ->01-01-2021 and
todate-> 31-12-2022.
expected dates are like below
[
{
"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"
}
]
Can anyone plz help me to resolve this issue?
解决方案 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 before recurrenceList.push
to ensure it only adds the entry to the list if the startDate is on or after dateFrom
- 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 = 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>
这篇关于如何过滤当月的提醒日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆