How can I find closest date value from an array according to the current date?


How can I find closest date value from an array according to the current date?



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();




4 Answers
4



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]);





Some punctuation might help. ;-)
– RobG
Jun 30 at 7:21





@Joven28 it's work fine in all browsers, but it doesn't work in IE and Microsoft Edge
– Shifut Hossain
2 days ago



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.


const dates = [
'1980-7-12',
'2001-1-5',
'2018-4-12',
'1999-3-25'
];



Here's a little function that can help you:
It has 2 parameters ->
a) dates which are your array of dates &&
b) which is the value you're comparing it to.


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
}



See Array.prototype.reduce() for more info.


Array.prototype.reduce()




// 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)





You should probably use Math.abs(Date.now() - new Date(date)) to get the closest for either side of the current date. ;-) Also, use semicolons and do not trust the built-in parser with non-standard strings.
– RobG
Jun 30 at 7:25



Math.abs(Date.now() - new Date(date))





Good catch, cheers 😁 @RobG
– Arman Charan
Jun 30 at 7:41



You can sort the dates array by the absolute difference between current date and extract the first item:


dates


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);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV