I have a array of objects containing dates, if a user added another start and end date it cannot overlap with any existing dates in the array.
我有一个包含日期的对象数组,如果用户添加了另一个开始和结束日期,它不能与数组中的任何现有日期重叠。
Using Moment
I have this loop but no alert is fired when I input a date that overlaps
使用Moment我有这个循环但是当我输入重叠的日期时没有触发警报
Using the input dates, startDate: "2013-09-02T00:00:00", endDate: "2015-05-05T00:00:0
, the loop over the last array item should fire the alert, as the dates overlap.
使用输入日期startDate:“2013-09-02T00:00:00”,endDate:“2015-05-05T00:00:0,最后一个数组项的循环应该触发警报,因为日期重叠。
let resultData = data.row;
let array = this.props.sectionData;
let inputStartDate = Moment(resultData.startDate, "DD/MM/YYYY");
let inputEndDate = Moment(resultData.endDate, "DD/MM/YYYY");
array.forEach( function (i) {
if(i.startDate && i.endDate) {
let startDate = Moment(i.startDate, "DD/MM/YYYY");
let endDate = Moment(i.endDate, "DD/MM/YYYY");
if (inputStartDate.isBetween(startDate, endDate) || inputEndDate.isBetween(startDate, endDate)) {
alert('date range cannot overlap');
}
}
});
My data:
[
{"id": 3,
"startDate": 2013-09-01T00:00:00,
"endDate": 2013-09-01T00:00:00,
"description": null,
"userId": 900,
"userName": "",
"documents": [],
},
{
"id": 5,
"startDate": 2013-09-01T00:00:00,
"endDate": 2013-09-01T00:00:00,
"description": null,
"userId": 1,
"userName": "",
"documents": [],
"contributors": []
},
{
"id": 6,
"startDate": "2013-09-01T00:00:00",
"endDate": "2014-08-31T00:00:00",
"description": "content",
"userId": 1,
"userName": ""
},
1 个解决方案
#1
0
Let moment parse the object. See https://plnkr.co/edit/OqKNDsSWvcMc3PXOcu9F?p=preview for a working example.
让我们解析对象。有关工作示例,请参阅https://plnkr.co/edit/OqKNDsSWvcMc3PXOcu9F?p=preview。
Explanation:
let inputStartDate = moment("2013-09-02T00:00:00");
let inputEndDate = moment("2015-05-05T00:00:00");
As ADyson explained your were passing in the incorrect formats. Also you are missing quotes in your test data around your dates for ids 3 and 5.
正如ADyson解释说你传递的格式不正确。此外,您在测试数据中缺少ids 3和5日期的引号。
#1
0
Let moment parse the object. See https://plnkr.co/edit/OqKNDsSWvcMc3PXOcu9F?p=preview for a working example.
让我们解析对象。有关工作示例,请参阅https://plnkr.co/edit/OqKNDsSWvcMc3PXOcu9F?p=preview。
Explanation:
let inputStartDate = moment("2013-09-02T00:00:00");
let inputEndDate = moment("2015-05-05T00:00:00");
As ADyson explained your were passing in the incorrect formats. Also you are missing quotes in your test data around your dates for ids 3 and 5.
正如ADyson解释说你传递的格式不正确。此外,您在测试数据中缺少ids 3和5日期的引号。