返回与现在最接近的时间的项目JavaScript/Moment.js

返回与现在最接近的时间的项目JavaScript/Moment.js,javascript,vue.js,momentjs,Javascript,Vue.js,Momentjs,我正在开发一个与日志和时间相关的应用程序。我有一个包含对象日志的数组。我想找到签入时间为最近对象的属性的对象。因此,如果我有两个对象,一个是签入时间为2018-04-05 08:04:12的对象,另一个是签入时间为2018-04-05 10:02:12的对象,我希望找到2018-04-05 10:02:12的对象,因为这是离现在最近的时间 我尝试了使用Moment.js库的方法,但没有成功。我正在使用Vue框架 更新 我已经尝试了下面的一个答案,但仍然没有得到明确的答案。有什么我看不到的吗 代码

我正在开发一个与日志和时间相关的应用程序。我有一个包含对象日志的数组。我想找到签入时间为最近对象的属性的对象。因此,如果我有两个对象,一个是签入时间为2018-04-05 08:04:12的对象,另一个是签入时间为2018-04-05 10:02:12的对象,我希望找到2018-04-05 10:02:12的对象,因为这是离现在最近的时间

我尝试了使用Moment.js库的方法,但没有成功。我正在使用Vue框架

更新

我已经尝试了下面的一个答案,但仍然没有得到明确的答案。有什么我看不到的吗

代码

有关更多信息,请参见和

//输入。 常量输入=[ {id:1,签入时间:2030-05-05 16:22:02},//2030年5月5日下午4点。 {id:2,签入时间:2030-05-05 08:22:02},//2030年5月5日上午8点。 {id:3,登记时间:2000-05-05 13:22:02},//2030年5月5日下午1点。 {id:4,登记时间:2000-05-05 11:22:02},//2030年5月5日上午11点。 ] //是在现在之前 const isBeforeNow=x=>Date.now>x //生成密钥。 const generateKey=x=>Math.absDate.now*1-x*1 //离现在最近。 const closestFromNow=次数,限制=>{ const m=new-Maptimes.mapx=>{ 让日期=新日期x.checkin\u时间 如果限制=='before'{ 如果IsBeforeKnowDate=generateKeydate else日期=未定义 } 如果限制=='after'{ 如果!isBeforeNowdate日期=generateKeydate else日期=未定义 } 否则{ 日期=生成日期 } 返回[日期,x] } m、 删除未定义 返回m.getMath.min…m.keys } //证明。 console.log'Closest Before Now',closestFromNowinput',Before'//ID 3。 console.log'Closest After Now',closestFromNowinput',After'//ID 2。 console.log“一般情况下最接近现在1”,closestFromNowinput//ID 3。 console.log'closefrom Now In Generate 2',closestfrom Now[…输入,{id:11,签入时间:2020-05-05 11:22:02}]//id 11。 方法:{ 获取日志{ var bigestElem=null; var closestTime=0; var elemTime=0; this.users.forEachelement=>{ element.logs.forEachlog=>{ elemTime=new Datelog.checkin\u time.getTime iflog.finished==false&&elemTime>closestime{ closestTime=elemTime; bigestElem=对数; } this.Logs.pushlog; } }; console.logbigestElem; 返回比格斯特伦 }
},您可以创建一个助手函数,如下所示:

const input = [
  {id: 1, checkin_time: "2030-05-05 10:22:02"}, // 10 AM. 
  {id: 2, checkin_time: "2030-05-05 08:22:02"} // 8 AM.
]


function findNearestTime(times){
  let currentTime = Date.now();
  let diff = times.map(time => {
    return Math.abs(currentTime - Date.parse(time.checkin_time));
  })

  let i = diff.indexOf(Math.min(...diff));

  return times[i].checkin_time;
}

let latest = findNearestTime(input);
alert("nearest time is :" + latest)
这是你的电话号码


参考:和

您尝试了什么,为什么不起作用?这不会直接起作用,因为时间是对象的属性,而不是数组中的项目。我将用一个示例对象更新我的问题。不幸的是,此解决方案返回2030-05-05 10:22:02最远时间@ArmanCharan感谢您指出。。。我忽略了这一年。现在是2030年:-不是12018年…嗨,我忘了在《纽约时报》上提到我的数据对象。我得到一个未定义的值,因为checkin_time属性是一个字符串,而不是number/Date.nowSure。用新的日期包起来应该可以。看上面。我从中得到了未定义的信息。我将添加我的console.logs,也许我没有看到不正确的东西。好吧,我发现它只适用于比现在早几倍的时间。我希望有过去的时间,然后是最近的时间。我改进了算法。见上文。
const input = [
  {id: 1, checkin_time: "2030-05-05 10:22:02"}, // 10 AM. 
  {id: 2, checkin_time: "2030-05-05 08:22:02"} // 8 AM.
]


function findNearestTime(times){
  let currentTime = Date.now();
  let diff = times.map(time => {
    return Math.abs(currentTime - Date.parse(time.checkin_time));
  })

  let i = diff.indexOf(Math.min(...diff));

  return times[i].checkin_time;
}

let latest = findNearestTime(input);
alert("nearest time is :" + latest)