From 4dac13bcc0d14e2f26656e958781854a70426a3c Mon Sep 17 00:00:00 2001 From: sfja Date: Wed, 2 Jul 2025 00:17:12 +0200 Subject: [PATCH] fix dates --- public/index.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/public/index.js b/public/index.js index d3d0f36..7fed8a1 100644 --- a/public/index.js +++ b/public/index.js @@ -8,13 +8,13 @@ timeUtcSpan.innerText = new Date() // .toLocaleTimeString(["en-UK"], { hour: "2-digit", minute: "2-digit" }); const weekDayNames = [ + "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", - "Sunday", ]; const monthNames = [ @@ -32,29 +32,37 @@ const monthNames = [ "December", ]; +const utcWeekDayName = (utcDate) => weekDayNames[utcDate.getUTCDay()]; +const utcDayOfMonth = (utcDate) => utcDate.getUTCDate(); +const utcMonthName = (utcDate) => monthNames[utcDate.getUTCMonth()]; + +const utcDateFromString = (dateString) => + new Date( + new Date(dateString).getTime() - + new Date().getTimezoneOffset() * 60 * 1000, + ); + async function makeScheduleTable() { const timeslotData = await fetch("timeslots.json") .then((res) => res.json()); + console.log(timeslotData); + const timeslots = timeslotData .map(({ date, times }) => ({ dateString: date, - // yea soo this hack makes the Date UTC - date: new Date( - new Date(date).getTime() - - new Date().getTimezoneOffset() * 60 * 1000, - ), + date: utcDateFromString(date), times, })) .toSorted((a, b) => a.date.getTime() - b.date.getTime()); + console.log(timeslots); + let tableHtml = ` ${ timeslots.map((ts) => - `${ - weekDayNames[ts.date.getUTCDay()] - } ${ts.date.getUTCDate()}. ${ - monthNames[ts.date.getUTCMonth()] + `${utcWeekDayName(ts.date)} ${utcDayOfMonth(ts.date)}. ${ + utcMonthName(ts.date) }` ) .join("")