[英]Javascript: Get object with date range in array which matches to the current time
Please correct me if this question has already been asked on Stack, but I haven't found any answer which would solve my problem.如果已经在 Stack 上问过这个问题,请纠正我,但我没有找到任何可以解决我的问题的答案。 I want to display only the object of my array, where the date range, consisting of
timeStart
and timeEnd
, matches to the current time.我只想显示数组的对象,其中由
timeStart
和timeEnd
组成的日期范围与当前时间匹配。 Let's say the current time would be 2020-10-22T16:35:45+01:00
, the function should display Steve (see the example given below) it's appointment.假设当前时间是
2020-10-22T16:35:45+01:00
,该函数应该显示Steve (参见下面给出的示例)它的约会。 How can I search for the object where the current time is in between the given date ranges?如何搜索当前时间在给定日期范围之间的对象?
const currentTime = Date.now(); // 2020-10-22T16:35:45+01:00
This is the array I'm working with:这是我正在使用的数组:
[
{
name: "Thomas",
timeStart: "2020-10-22T16:00:00+0100",
timeEnd: "2020-10-22T16:15:00+01:00"
},
{
name: "Marc",
timeStart: "2020-10-22T16:15:00+0100",
timeEnd: "2020-10-22T16:30:00+01:00"
},
{
name: "Steve",
timeStart: "2020-10-22T16:30:00+0100",
timeEnd: "2020-10-22T16:45:00+01:00"
}
]
Does this work for you?这对你有用吗? It searches every date and makes sure that the current date is between the start and end.
它搜索每个日期并确保当前日期介于开始和结束之间。
const currentTime = Date.now(); // 2020-10-22T16:35:45+01:00 const dates = [{ name: "Thomas", timeStart: "2020-10-22T16:00:00+0100", timeEnd: "2020-10-22T16:15:00+01:00" }, { name: "Marc", timeStart: "2020-10-22T16:15:00+0100", timeEnd: "2020-10-22T16:30:00+01:00" }, { name: "Steve", timeStart: "2020-10-22T16:30:00+0100", timeEnd: "2020-10-22T16:45:00+01:00" } ]; // If you want only one result, use find instead of filter const validDates = dates.filter((obj) => { // Converts strings to Dates let startDate = new Date(obj.timeStart); let endDate = new Date(obj.timeEnd); // Makes sure the current time is after the start date and before the end date return currentTime >= startDate && currentTime <= endDate; }); console.log(validDates); // To get name, use validDates[0].name (for filter, see above) or validDates.name (for find)
It will be something like this:它会是这样的:
const currentTime = Date.now();
const resultArray=yourArray.filter(item=>{
// you have to format dates to be able to compare them
return item.timeStart < currentTime && item.timeEnd > currentTime
});
Compare the start, end and current dates inside a find
loop.比较
find
循环中的开始、结束和当前日期。
If the comparison returns true then the current object will be returned, otherwise undefined
.如果比较返回 true 则返回当前对象,否则返回
undefined
。
const now = new Date("2020-10-22T16:17:00+01:00"); // 16:17. Should be Marc. const dates = [ { name: "Thomas", timeStart: "2020-10-22T16:00:00+0100", timeEnd: "2020-10-22T16:15:00+01:00" }, { name: "Marc", timeStart: "2020-10-22T16:15:00+0100", timeEnd: "2020-10-22T16:30:00+01:00" }, { name: "Steve", timeStart: "2020-10-22T16:30:00+0100", timeEnd: "2020-10-22T16:45:00+01:00" } ]; const result = dates.find(({ timeStart, timeEnd }) => { const start = new Date(timeStart); const end = new Date(timeEnd); return start <= now && end > now; }); console.log(result);
试试这样 yourArray.filter(item => currentTime >= item.timeStart && currenttime <= timeEnd)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.