Reputation: 1699
I want to get the next closest date on this array according to the current date.
var dates = [
'Aug 18, 2018 03:24:00',
'August 19, 2018 03:24:00',
'September 17, 2018 03:24:00',
'September 14, 2018 03:24:00',
'August 18, 2018 03:24:00',
'July 16, 2018 03:24:00',
'July 15, 2018 03:24:00',
'December 15, 2018 03:24:00',
'July 13, 2018 03:24:00',
];
var now = new Date();
Upvotes: 0
Views: 4498
Reputation: 7822
You can use moment.js library to convert your dates in any format you want.
You can achieve that simply by mapping over your array and by using the
moment(yourDate).format(your format) functionality.
Here's a little function that can help you:
It has 2 parameters ->
.
const dates = [
'1980-7-12',
'2001-1-5',
'2018-4-12',
'1999-3-25'
];
const nearestDate = (dates, target) => {
if (!target
) target = Date.now()
else if (target instanceof Date) target = target.getTime()
let nearest = Infinity
let winner = -1
dates.forEach(function (date, index) {
if (date instanceof Date) date = date.getTime()
let distance = Math.abs(date - target)
if (distance < nearest) {
nearest = distance
winner = index
}
})
return winner
}
Upvotes: 0
Reputation: 1702
You can sort the dates
array by the absolute difference between current date and extract the first item:
var dates = [
'Aug 18, 2018 03:24:00',
'August 19, 2018 03:24:00',
'September 17, 2018 03:24:00',
'September 14, 2018 03:24:00',
'August 18, 2018 03:24:00',
'July 16, 2018 03:24:00',
'July 15, 2018 03:24:00',
'December 15, 2018 03:24:00',
'July 13, 2018 03:24:00',
];
var now = new Date();
var [ closest ] = dates.sort((a,b) => {
const [aDate, bDate] = [a,b].map(d => Math.abs(new Date(d) - now));
return aDate - bDate;
});
console.log(closest);
Upvotes: 0
Reputation: 5797
See Array.prototype.reduce()
for more info.
// Input.
const dates = ['July 16, 1995 03:24:00','Aug 18, 1995 03:24:00','August 19, 1995 03:24:00','September 17, 1995 03:24:00','September 14, 1995 03:24:00','August 18, 1995 03:24:00','July 16, 1995 03:24:00','December 15, 1995 03:24:00','July 13, 1995 03:24:00']
// Closest Date.
const closestDate = dates => dates.reduce((x, date) => {
const distance = Math.abs(Date.now() - new Date(date))
if (!x.distance || distance < x.distance) return {distance, date}
return x
}, {}).date
// Output + Proof.
const output = closestDate(dates)
console.log(output)
Upvotes: 0
Reputation: 769
First you need to convert each date into a timestamp then subtract each of them by the current date and store the timestamp difference in the temp array then get the index of the minimum value and use the index to access the closest date in the original array.
var dates = [
'July 16, 1995 03:24:00',
'Aug 18, 1995 03:24:00',
'August 19, 1995 03:24:00',
'September 17, 1995 03:24:00',
'September 14, 1995 03:24:00',
'August 18, 1995 03:24:00',
'July 16, 1995 03:24:00',
'December 15, 1995 03:24:00',
'July 13, 1995 03:24:00',
]
var temp = dates.map(d => Math.abs(new Date() - new Date(d).getTime()));
var idx = temp.indexOf(Math.min(...temp));
console.log(dates[idx]);
Upvotes: 2