如何查找ISO格式中最接近今日的日期?含数组筛选场景
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 wherecustomerAuthorizedTimeis exactlynull. - Finding the closest time:
Array.reduce()iterates through the filtered list, comparing the absolute time difference (in milliseconds) between eachstartTimeand 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
相关产品推荐
相关产品推荐

