仅比较 moment 对象中的时间部分
Compare only timepart in moment object
我只想比较 moment 对象中的时间部分。我已经包含了一个目前无法按预期工作的代码段。过滤器功能未正常工作。我想过滤掉所有开始时间设置在 07:00 之前或结束时间设置在 19:00 之后的日期。我还收到一个已弃用的警告,我无法真正找出原因。感谢大家的帮助
var arrayOfWorkDates = [{
start: "2018-05-02T07:00:00Z",
end: "2018-05-02T09:00:00Z"
}, {
start: "2018-05-02T019:00:00Z",
end: "2018-05-02T21:00:00Z"
}, {
start: "2018-05-02T010:00:00Z",
end: "2018-05-02T11:00:00Z"
}];
console.log(arrayOfWorkDates);
var StartHour = moment("07:00", "HH:mm");
var EndHour = moment("19:00", "HH:mm");
var filteredWokrHours = arrayOfWorkDates.filter(function(el) {
return moment(el.start).isBefore(StartHour) || moment(el.end).isAfter(EndHour);
});
if(filteredWokrHours.length > 0){console.log("success: " + filteredWokrHours.length);}else{console.log("fail");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
这样解决了。
var format = "HH:mm:ss";
var hourFormat = "HH:mm";
var maxStartHour = moment("07:00:00", format);
var minEndHour = moment("19:00:00", format);
var arrayOfWorkDates = [
{
start: "2018-02-24T14:00:00",
end: "2018-02-24T15:00:00"
},
{
start: "2018-02-24T05:00:00",
end: "2018-02-24T06:00:00"
},
{
start: "2018-02-24T20:00:00",
end: "2018-02-24T21:00:00"
}
];
var filteredWokrHours = arrayOfWorkDates.filter(function(el) {
var start = moment(el.start).format(hourFormat);
var end = moment(el.end).format(hourFormat);
var checkHourStart = moment(start, format);
var checkHourEnd = moment(end, format);
return checkHourStart.isBefore(maxStartHour) || checkHourEnd.isAfter(minEndHour);
});
if(filteredWokrHours.length > 0){console.log("success: " + filteredWokrHours.length);}else{console.log("fail");}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
我只想比较 moment 对象中的时间部分。我已经包含了一个目前无法按预期工作的代码段。过滤器功能未正常工作。我想过滤掉所有开始时间设置在 07:00 之前或结束时间设置在 19:00 之后的日期。我还收到一个已弃用的警告,我无法真正找出原因。感谢大家的帮助
var arrayOfWorkDates = [{
start: "2018-05-02T07:00:00Z",
end: "2018-05-02T09:00:00Z"
}, {
start: "2018-05-02T019:00:00Z",
end: "2018-05-02T21:00:00Z"
}, {
start: "2018-05-02T010:00:00Z",
end: "2018-05-02T11:00:00Z"
}];
console.log(arrayOfWorkDates);
var StartHour = moment("07:00", "HH:mm");
var EndHour = moment("19:00", "HH:mm");
var filteredWokrHours = arrayOfWorkDates.filter(function(el) {
return moment(el.start).isBefore(StartHour) || moment(el.end).isAfter(EndHour);
});
if(filteredWokrHours.length > 0){console.log("success: " + filteredWokrHours.length);}else{console.log("fail");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>
这样解决了。
var format = "HH:mm:ss";
var hourFormat = "HH:mm";
var maxStartHour = moment("07:00:00", format);
var minEndHour = moment("19:00:00", format);
var arrayOfWorkDates = [
{
start: "2018-02-24T14:00:00",
end: "2018-02-24T15:00:00"
},
{
start: "2018-02-24T05:00:00",
end: "2018-02-24T06:00:00"
},
{
start: "2018-02-24T20:00:00",
end: "2018-02-24T21:00:00"
}
];
var filteredWokrHours = arrayOfWorkDates.filter(function(el) {
var start = moment(el.start).format(hourFormat);
var end = moment(el.end).format(hourFormat);
var checkHourStart = moment(start, format);
var checkHourEnd = moment(end, format);
return checkHourStart.isBefore(maxStartHour) || checkHourEnd.isAfter(minEndHour);
});
if(filteredWokrHours.length > 0){console.log("success: " + filteredWokrHours.length);}else{console.log("fail");}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js"></script>