Week of year

Former Member
Former Member
Hello all,

sorry for my bad english, because i´m from Germany.

My question:
Can i display the number of week in a WatchFace?
I hope you understand what i mean.
Now we have week 13 (https://whatweekisit.com/)
This is what i mean.

Great
Heiko
  • Yes, you have to calculate the value though. It isn't that difficult. I can post code.
  • Former Member
    Former Member over 9 years ago
    1784

    Hello TRAVIS.VITEK,

    That would be very happy if you could give me an example.
    So I an approach as it can be done.
    Thank you very much for your efforts.

    greeting
    Heiko
  • Former Member
    Former Member over 9 years ago
    Hello TRAVIS.VITEK,

    many thanks for the code.
    The works perfectly and is perfect.
    I would never have managed that so that to write.
    I am more in PHP / MySQL and JavaScript on the go, since the reaction would have been a problem for me.

    Thanks again for that.

    greeting Heiko
  • Former Member
    Former Member over 8 years ago
    Yes, you have to calculate the value though. It isn't that difficult. I can post code.


    I could really like a copy of this code too.

    Thanks a lot in advance
  • Hi Travis i am interresting in the code too

    Thank's
  • Former Member
    Former Member over 8 years ago
    Why the conspiracy all of a sudden?

    using Toybox.Time.Gregorian as Greg;
    ...
    var dateInfo = Greg.info(Time.now(), Time.FORMAT_SHORT);
    var dayInAYear = (Time.now().value() - Greg.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0}).value()) / 86400;
    Sys.println("today is "+ dayInAYear +"th day of the year, "+ (dayInAYear / 7 + 1) +"th week");
    ...


    Is there something particular I'm missing?
  • I guess it could vary based on what "week of year" is defined as for an app.

    For example, is the first week from 1/1 to the next sunday or monday (based on a setting like "start of week"), and each week counted after that is from sunday to saturday (or monday to sunday based on the "start of week")

    Or is a "week" from whatever day 1/1 is on to the next one?
  • Former Member
    Former Member over 8 years ago
    Why the conspiracy all of a sudden?

    using Toybox.Time.Gregorian as Greg;
    ...
    var dateInfo = Greg.info(Time.now(), Time.FORMAT_SHORT);
    var dayInAYear = (Time.now().value() - Greg.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0}).value()) / 86400;
    Sys.println("today is "+ dayInAYear +"th day of the year, "+ (dayInAYear / 7 + 1) +"th week");
    ...


    Is there something particular I'm missing?


    This is a good starting point, but the weeknumber is not calculated correct.

    It seems a bit complicated to calculate, especially for the years containing 53 weeks.

    Anybody have a final and complete solution so I don't have to re-invent the wheel

    Thanks in advance
    https://en.wikipedia.org/wiki/Week#Week_numbering
  • Former Member
    Former Member over 8 years ago
    Well, I never really did these calculations before since in Android Java there's always trustworthy Calendar.getInstance.get(Calendar. WEEK_OF_YEAR), but I do like to reinvent wheels if it seems interesting enough 8)) According to provided Wiki, and I must admit, that this info was new to me, I'd change my code to this (checked to compile and run in simulator):

    ...
    Sys.println("today is "+ getWeekNumber(Time.now()) +"th week");
    Sys.println("01.01.2016 belongs to "+ getWeekNumber(Greg.moment({:year => 2016, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0})) +" week");
    Sys.println("03.01.2016 belongs to "+ getWeekNumber(Greg.moment({:year => 2016, :month => 1, :day => 3, :hour => 0, :minute => 0, :second => 0})) +" week");
    Sys.println("04.01.2016 belongs to "+ getWeekNumber(Greg.moment({:year => 2016, :month => 1, :day => 4, :hour => 0, :minute => 0, :second => 0})) +" week");
    ...
    function getWeekNumber(moment) {
    var dateInfo = Greg.info(moment, Time.FORMAT_SHORT);
    var firstDayOfTheYear = Greg.moment({:year => dateInfo.year, :month => 1, :day => 1, :hour => 0, :minute => 0, :second => 0});
    var dayInAYear = (moment.value() - firstDayOfTheYear.value()) / 86400;
    var weekNumber = dayInAYear / 7;
    var firstDayDoW = Greg.info(firstDayOfTheYear, Time.FORMAT_SHORT).day_of_week;
    // var lastDayOfFirstWeek = 9 - firstDayDoW; //my bad: Friday = 6, Satudray = 7, Sunday = 1
    var lastDayOfFirstWeek = (9 - firstDayDoW) % 7;
    return weekNumber == 0 && (firstDayDoW == 1 || firstDayDoW >= 6) && dateInfo.day <= lastDayOfFirstWeek ?
    getWeekNumber(Greg.moment({:year => dateInfo.year - 1, :month => 12, :day => 31, :hour => 0, :minute => 0, :second => 0})) :
    (weekNumber + 1);
    }