Giter VIP home page Giter VIP logo

Comments (8)

justin-schroeder avatar justin-schroeder commented on August 11, 2024

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

from tempo.

DJWassink avatar DJWassink commented on August 11, 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

from tempo.

justin-schroeder avatar justin-schroeder commented on August 11, 2024

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

from tempo.

DJWassink avatar DJWassink commented on August 11, 2024

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.

from tempo.

justin-schroeder avatar justin-schroeder commented on August 11, 2024

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.

from tempo.

DJWassink avatar DJWassink commented on August 11, 2024

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

from tempo.

justin-schroeder avatar justin-schroeder commented on August 11, 2024

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.

from tempo.

romanhrynevych avatar romanhrynevych commented on August 11, 2024

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 🙂

from tempo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.