简体   繁体   English

查找最近的时间 [javascript]

[英]Find the closest hour [javascript]

I have a list of these items:我有这些项目的清单:

hours = ['19:30', '20:10', '20:30', '21:00', '22:00']

Assuming that now it's 20:18, how can I get the '20:10' item from the list?假设现在是 20:18,我怎样才能从列表中获取“20:10”项目? I want to use this to find the currently running show in a TV Guide.我想用它在电视指南中查找当前正在播放的节目。

First we should parse it to datetime in some way:首先,我们应该以某种方式将其解析为日期时间:

function parseTime(str) {
    var values = str.split(':');
    var hour = parseInt(values[0]);
    var minutes = parseInt(values[1]);
    var d = new Date();
    d.setHours(hour);
    d.setMinutes(minutes);
    return d;
}
var now = new Date();
var bestMatch = undefined;
var bestMatchDiff = undefined;

And finally:最后:

for(var i=0; i<hours.length; i++) {
    var parsedTime = parseTime(hours[i]);
    var diff = Math.abs(now - parsedTime);
    if (!bestMatchDiff || bestMatchDiff>diff) {
        bestMatch = parsedTime;
        bestMatchDiff = diff;
    }
}

bestMatch would be the closest time. bestMatch 将是最接近的时间。 This is not the currently running show now.这不是现在正在播放的节目。 For that, you need to ignore times that are yet to come:为此,您需要忽略尚未到来的时间:

for(var i=0; i<hours.length; i++) {
    var parsedTime = parseTime(hours[i]);
    var diff = Math.abs(now - parsedTime);
    if (now<parsedTime) {
        continue;
    }
    if (!bestMatchDiff || bestMatchDiff>diff) {
        bestMatch = parsedTime;
        bestMatchDiff = diff;
    }
}

But keep in mind this might return undefined even if your list is not empty.但请记住,即使您的列表不为空,这也可能返回undefined

    var hours = ['19:30', '20:10', '20:30', '21:00', '22:00']
    var diffrenceTime
    var selectedShow
    var d1 = new Date()
    var currentHH = 20
    var currentMM = 18
    d1.setHours(currentHH, currentMM, 0)
    hours.forEach(v => {
        var d2 = new Date()
        var hh = v.split(':')[0]
        var mm = v.split(':')[1]
        d2.setHours(hh, mm, 0)
        if (diffrenceTime == undefined) {
            diffrenceTime = d2 - d1
            selectedShow = v
        }
        if (d2 - d1 < 0 && d2 - d1 >= diffrenceTime) {
            diffrenceTime = d2 - d1
            selectedShow = v
        }
    })
    console.log(selectedShow)

To find the currently running show (of course more validations need to be added):要查找当前正在运行的节目(当然需要添加更多验证):

const currentShow = hours[
  hours.findIndex(
    (c) => new Date(`01/01/2000 ${c}`) - new Date(`01/01/2000 20:31`) >= 0
  ) - 1
];

To find the next show:要查找下一个节目:

const nextShow = hours.find(
  (c) => new Date(`01/01/2000 ${c}`) - new Date(`01/01/2000 20:31`) >= 0
);

Well, you could do something like this好吧,你可以做这样的事情

 var originalArray = ['19:30', '20:10', '20:30', '21:00', '22:00']; var newArray = originalArray.map(i=>{ return i.split(":") }) newArray.forEach((k, idx)=>{newArray[idx] = parseInt(k[0]) + parseInt(k[1])/60}) console.log(newArray); var time = "20:18".split(':'); var t = parseInt(time[0])+ parseInt(time[1])/60; console.log(t); var closestTimeIndex = 0, closestDistance = Math.abs(t-newArray[closestTimeIndex]); for(var m=1; m<newArray.length;m++){ if(Math.abs(newArray[m]-t) < closestDistance){ closestDistance = Math.abs(newArray[m]-t); closestTimeIndex = m; } } console.log("colsest time is: " + originalArray[closestTimeIndex]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM