You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查找ISO格式中最接近今日的日期?含数组筛选场景

How to Find the Closest Appointment by Start Time (Filtered by Null Authorization Time)

Got it, let's break this down step by step. First, we need to filter the items where customerAuthorizedTime is null, then find the one whose startTime is closest to the current date. Here's how you can do it with both vanilla JavaScript and Moment.js:

Vanilla JavaScript Solution

You don't need any extra libraries for this—JavaScript's built-in Date object handles ISO 8601 timestamps perfectly. Here's the code:

const data = [
  { 'appointmentId': 403749, 'masterAppointmentId': 403749, 'memberPartyRoleId': 1262903, 'description': 'Training (30 minutes)', 'startTime': '2019-06-09T22:00:00-04:00', 'mustCancelBy': '2019-06-09T22:00:00-04:00', 'activityTransactionId': 19726389, 'customerAuthorizedTime': '2019-06-09T00:33:02-04:00', },
  { 'appointmentId': 403750, 'masterAppointmentId': 403750, 'memberPartyRoleId': 1262903, 'description': 'Training (30 minutes)', 'startTime': '2019-06-09T21:00:00-04:00', 'mustCancelBy': '2019-06-09T21:00:00-04:00', 'activityTransactionId': 19726390, 'customerAuthorizedTime': null, },
  { 'appointmentId': 403748, 'masterAppointmentId': 403748, 'memberPartyRoleId': 1262903, 'description': 'Training (30 minutes)', 'startTime': '2019-06-09T20:00:00-04:00', 'mustCancelBy': '2019-06-09T20:00:00-04:00', 'activityTransactionId': 19726388, 'customerAuthorizedTime': null, },
  { 'appointmentId': 403747, 'masterAppointmentId': 403747, 'memberPartyRoleId': 1262903, 'description': 'Training (30 minutes)', 'startTime': '2019-06-09T19:00:00-04:00', 'mustCancelBy': '2019-06-09T19:00:00-04:00', 'activityTransactionId': 19726387, 'customerAuthorizedTime': '2019-06-09T00:13:18-04:00', }
];

// 1. Filter out items where customerAuthorizedTime is null
const filteredAppointments = data.filter(item => item.customerAuthorizedTime === null);

if (filteredAppointments.length === 0) {
  console.log("No appointments with null customerAuthorizedTime found.");
} else {
  const currentTime = new Date();
  
  // 2. Find the appointment with startTime closest to current time
  const closestAppointment = filteredAppointments.reduce((prev, curr) => {
    // Convert ISO string to Date object and calculate absolute time difference
    const prevTimeDiff = Math.abs(new Date(prev.startTime) - currentTime);
    const currTimeDiff = Math.abs(new Date(curr.startTime) - currentTime);
    
    // Return the item with the smaller time difference
    return prevTimeDiff < currTimeDiff ? prev : curr;
  });

  console.log("Closest appointment:", closestAppointment);
}

How it works:

  • Filtering: We use Array.filter() to keep only items where customerAuthorizedTime is exactly null.
  • Finding the closest time: Array.reduce() iterates through the filtered list, comparing the absolute time difference (in milliseconds) between each startTime and the current time. The item with the smallest difference is kept as the result.

Moment.js Solution

If you're already using Moment.js in your project, it makes time handling even cleaner. Here's how to implement it:

const data = [ /* Same dataset as above */ ];
const moment = require('moment'); // For Node.js; use a script tag in browsers

const filteredAppointments = data.filter(item => item.customerAuthorizedTime === null);

if (filteredAppointments.length === 0) {
  console.log("No appointments with null customerAuthorizedTime found.");
} else {
  const currentTime = moment();
  
  const closestAppointment = filteredAppointments.reduce((prev, curr) => {
    // Use Moment.js to calculate absolute time difference
    const prevTimeDiff = Math.abs(moment(prev.startTime).diff(currentTime));
    const currTimeDiff = Math.abs(moment(curr.startTime).diff(currentTime));
    
    return prevTimeDiff < currTimeDiff ? prev : curr;
  });

  console.log("Closest appointment:", closestAppointment);
}

Why Moment.js helps:

  • Moment.js handles time zones and ISO formatting seamlessly, so you don't have to worry about edge cases in date parsing.
  • The diff() method directly returns the difference in milliseconds (by default), making the comparison straightforward.

内容的提问来源于stack exchange,提问作者nyphur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:15:56