If I have an array like:
let array = ["Friday, 11 February","Tuesday, 10 February","Friday, 04 March", "Thursday, 17 February", "Today", "Monday, 9 February"];
.
What I try to do is to find the closest date to today or the smaller and I don't want to take "Today".
Until now I tried:
let array = ["Friday, 11 February","Tuesday, 10 February","Friday, 04 March", "Thursday, 17 February", "Today", "Monday, 9 February"];
array = array.map(el => el.split(", ").slice(1).join()).filter(el =>el)
let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
let closest = "";
for(let i = 0; i < array.length; i ){
for(let j = 1; j < array.length; j ){
let numberOne = array[i].match(/\d /)[0];
let monthOne = array[i].match(/\b[a-zA-Z] \b/g)[0];
let numberTwo = array[j].match(/\d /)[0];
let monthTwo = array[j].match(/\b[a-zA-Z] \b/g)[0];
if( (numberOne) < (numberTwo) && months.indexOf(monthOne) <= months.indexOf(monthTwo)){
closest = array[i]
}
}
}
console.log(closest)
Everything is ok but I want to know if I can transform in a Date and sort by date. Anyone have a solution to this or a cleaner way to find the closest date? The array is dynamic and until now I didn't find any date which is before today and to take that one. Thank you!
CodePudding user response:
First of all, here are one issue: year is not defined, so let's clarify we mean current year by transforming all dates to actual Date:
const now = new Date();
array.map(el => new Date(`${el} ${now.getFullYear()}`);
Then we can calculate differences from now (in milliseconds):
array.map(el => new Date(`${el} ${now.getFullYear()}`) - now.getTime());
Let's wrap them into Math.abs to easely find which one in smallest using Math.min():
const offsets = array.map(el => Math.abs(new Date(`${el} ${now.getFullYear()}`) - now.getTime()));
const closestIndex = offsets.findIndex(el => el === Math.min(...offsets));
closestIndex
- index of date with smallest offset which means "closest to now".
Full code (excluding "Today" from the beginning):
const array = ["Friday, 11 February", "Tuesday, 10 February", "Friday, 04 March", "Thursday, 17 February", "Today", "Monday, 9 February"].filter(el => el !== 'Today');
const now = new Date();
const offsets = array.map(el => Math.abs(new Date(`${el} ${now.getFullYear()}`) - now.getTime()));
const closestIndex = offsets.findIndex(el => el === Math.min(...offsets));
console.log(array[closestIndex]);
CodePudding user response:
I thought about the possibility that some dates could be the next year. The best example the end of December and the begining of January and I write the next code, which I hope can help someone
let array = [
"Tuesday, 03 January",
"Thursday, 05 January",
"Thursday, 29 December",
"Monday, 02 January",
"Friday, 30 December"
];
let now = new Date();
var datesArray = array.map((el) => el " " now.getFullYear());
datesArray = datesArray.map((el) => new Date(el));
datesArray = datesArray.map(el => {
if (el.getTime() < now.getTime()) {
return el = el.setFullYear(el.getFullYear() 1)
}else{
return el = el.setFullYear(el.getFullYear())
}
})
const closestIndex = datesArray.findIndex(el => el === Math.min(...datesArray));
console.log(array[closestIndex])