Suppose I create an all day event for Christmas day. I want this event to be 25 Dec regardless of which timezone you are in. I store the event date as "2023-12-25" on the server.
When I do new Date("2023-12-25") in the browser it's interpreted as a UTC date which is not what I want, for example if I'm in New York and I do
new Date("2023-12-25") // Sun Dec 24 2023 19:00:00 GMT-0500 (Eastern Standard Time)
Which means when I go to format the all day event it will format as 24 December which is not what I want.
Desired behaviour:
// If in New York
parseAllDayDate("2023-12-25") // Sun Dec 25 2023 00:00:00 GMT-0500 (Eastern Standard Time)
// If in London
parseAllDayDate("2023-12-25") // Mon Dec 25 2023 00:00:00 GMT+0000 (Greenwich Mean Time)
Also, please don't suggest dayjs or momentjs I would like to do this just with JS Date.
You can try this
parseAllDayDate(dateString) {
let dateStringSplit = dateString.split('-');
let date = new Date();
date.setUTCDate(dateStringSplit[2]);
date.setUTCMonth(dateStringSplit[1]);
date.setUTCFullYear(dateStringSplit[0]);
return date;
}