When finding a festivity that exists within an array of objects and when trying to print a user friendly message using toLocaleDateString
I get the festivity date with one day less than when toLocaleDateString
is executed
Please look at the following example
const date = "12/25/2020";
const [month, day, year] = date.split("/");
const newDate = `${year}-${month}-${day}`;
const holidays = [
{ celebration: "New year", date: "2020-12-31" },
{ celebration: "Christmas", date: "2020-12-25" },
{ celebration: "...", date: "2020-12-24" },
];
const holiday = holidays.find((holiday) => holiday.date === newDate);
if (holiday) {
const { celebration, date } = holiday;
const options = {
month: "long",
day: "2-digit",
};
const localDate = new Date(date).toLocaleDateString("en-US", options);
const message = `Date ${date} coincides with the holiday ${celebration} in date ${localDate}. Do you want to continue?`;
console.log(message);
}
I get Date 2020-12-25 coincides with the holiday Christmas in date December 24. Do you want to continue?
I expect Date 2020-12-25 coincides with the holiday Christmas in date December 25. Do you want to continue?
It's a subtle detail but i get 24
instead of 25