Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: weekNumber #44

Open
DJWassink opened this issue Mar 20, 2024 · 8 comments
Open

Feature request: weekNumber #44

DJWassink opened this issue Mar 20, 2024 · 8 comments

Comments

@DJWassink
Copy link

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.

@justin-schroeder
Copy link
Member

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

@DJWassink
Copy link
Author

DJWassink commented Mar 20, 2024

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

@justin-schroeder
Copy link
Member

@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 🪄

@DJWassink
Copy link
Author

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.

@justin-schroeder
Copy link
Member

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.

@DJWassink
Copy link
Author

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).

@justin-schroeder
Copy link
Member

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.

@romanhrynevych
Copy link
Contributor

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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants