tempo icon indicating copy to clipboard operation
tempo copied to clipboard

Feature request: weekNumber

Open DJWassink opened this issue 3 months ago • 8 comments

Thanks for this library its a delight to use!

One feature I am missing is for a way to get the week number of a given date + startOfWeekDay. Would be cool if this could also be included in the formatting tokens.

DJWassink avatar Mar 20 '24 08:03 DJWassink

Glad you enjoy it! By week number do you mean the week of the year? So Jan 1 being 1, Jan 10 be 2?

justin-schroeder avatar Mar 20 '24 13:03 justin-schroeder

Exactly!

I tried to see if I could create something but it looks more challenging then I expected. The closest I got so far was as following:

function weekNumber(givenDate: Date, startOfWeek: number) {
    const millisecondsInWeek = 604800000;

    const startDate = weekStart(givenDate, startOfWeek);
    const startOfYear = yearStart(startDate);

    const diff = startDate.getTime() - startOfYear.getTime();
    return Math.round(diff / millisecondsInWeek) + 1;
}

But this still fails for dates which weekStart end up in a previous year. For example: getWeekNumber(new Date(2013, 0, 0), 1) gives me week 53

DJWassink avatar Mar 20 '24 14:03 DJWassink

@DJWassink I think you actually have it about right except for new Date(2013, 0, 0) This is actually December 31 while argument 2 is month index, argument three is "day" with a starting number of 1 not 0 so 0 is actually Dec 31! Ha! Thanks JavaScript 🪄

justin-schroeder avatar Mar 20 '24 14:03 justin-schroeder

Ooh wow yeah 🤦‍♂️

However the bug still persist for other dates where the start of week is in the previous year. A more obvious one might be 31 dec 2013. new Date(2013, 11, 31). Which my code currently reports as week 53 but should be week 1.

I simply test with a big loop that goes through every day starting in 2013 till now and compare the moment.js week versus mine.

DJWassink avatar Mar 20 '24 14:03 DJWassink

Hmmm... wait — how could 2013-12-31 be in week 1? Are we saying that week one of 2024 was (assuming sunday is the first day of week) would be Dec 31 - Sat 6th even though it spans the year gap? Or is week one the 7th - 13th I guess there is some subjectivity and intuition there that would need to be nailed down.

justin-schroeder avatar Mar 20 '24 14:03 justin-schroeder

As far as I'm familiar with calendars the first week of 2024 would be Dec 31 - 6th of jan (on a sunday calendar). For example google calendar for me starts on a monday (dutch) and goes from 1 till 7 jan. More interesting 2023 for example has 2 - 8 jan as week 1 (assuming monday).

DJWassink avatar Mar 20 '24 15:03 DJWassink

Yeah, I suppose I haven’t thought much about it honestly I guess that does make sense. I wouldn’t wake up on the 7th of January and think to myself, "ok lets get this first week of the year underway". Ok! I think we could get this into tempo for sure.

justin-schroeder avatar Mar 20 '24 15:03 justin-schroeder

Also I think this must be relative to weekStartsOn, date-fns like logic link, because someone have start on monday, some on sunday, some on saturday 🙂

romanhrynevych avatar Mar 27 '24 19:03 romanhrynevych