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:...
