0

how calculate date from year, week number and weekday?

Date:= year(2019)+week(14)+weekday(0)

13 replies

null
    • agiorno
    • 5 yrs ago
    • Reported - view

    I mean, I don’t know how to make it correct, that example obviously doesn’t work lol

    • Sean
    • 5 yrs ago
    • Reported - view

    You might have to settle for an approximation. This is what I tried...

     

    let myYear := 2019;
    let myMonth := 0;
    let myWeek := 14;
    let myDay := 0;
    myMonth := floor(myWeek / 4);
    myDay := myWeek % 4 * 7;
    let myDate := date(myYear, myMonth, myDay);

     

    or

     

    myDate := date(myYear, floor(myWeek / 4), myWeek % 4 * 7)

    • agiorno
    • 5 yrs ago
    • Reported - view

    Sean! Thank you for your respons! Unfortunately it works incorrectly. In my case Weekday=0 means Monday, but your code turns it to day number.  Actually I need calculate date for each Friday in 2019 when week number is oddly

    • Sean
    • 5 yrs ago
    • Reported - view

    agiorno, you're welcome. If you consider 4/1/2019 (d/m/y) to be the first odd Friday, then you could use...

     

    date(year, month, day + multiples of 14) 

     

    to get the correct dates. I don't know if you want the dates all at once or as you need them. You could use a loop to get them all at once. As for using Day names instead of numbers, use the format() function. You can find the various format options here...

     

    https://ninoxdb.de/en/manual/calculations/reference-of-functions-and-language

    • Sean
    • 5 yrs ago
    • Reported - view

    Well, I just figured out the multiples of 14 will increment the weeks, but it doesn’t help with returning only the odd weeks. It will be later today before I can look at it again.

    • agiorno
    • 5 yrs ago
    • Reported - view

    Dear Sean! Thank you so much! I want to describe my goal. There are 4 doctors in clinic. All of them have different schedules depending on weeks( odds or even). Sometimes, administrators may choose wrong time for booking, so I need  to prevent them. Database should hide time interval which is not available for booking. That’s why I need to use different schedules depends of week.

    • Mconneen
    • 5 yrs ago
    • Reported - view

    @Agiorno

    The function

    week('Booking Date') 

    Will give you the week of the year.. I will have to dig through my notes.. There is a function that gives you the remainder.. So you can divide the week by 2 .. If there is a remainder.. it is ODD.. else even.. 

    I'm close.. just need that last step.. :) 

    • Mconneen
    • 5 yrs ago
    • Reported - view

    @Agiorno...   Here ya go..  The ever elusive modulo function.. :) 

    https://ninoxdb.de/en/forum/ideas-and-suggestions-5abb9f4f45eda7ea1e75ed02/suggest-include-modulo-function-in-documentation-5b05142a54369026ffa70518?post=5b05142a54369026ffa70519&page=0

    So.. 

    let mod := week('Booking Date') % 2;

    if mod = 0 then  
        .... week is even ... 
    else
       .... week is odd ....   

    • Sean
    • 5 yrs ago
    • Reported - view

    @Mconneen, Looks like I'm forever reinventing the wheel! That solves the odd/even as far as the calendar goes, but still leaves the requirement of constraining the schedule. Not enough information for me to say how.

    • up878844
    • 4 yrs ago
    • Reported - view

    While not a reply to help, I need a list of the first Mondays of each week between two dates.

    kgb

    • JGM
    • Jose_Monteiro
    • 4 yrs ago
    • Reported - view

    This block of code might seem a little awkward but it works and you can write it as function: my_function(year, week, week_day), keep it in the Options Panel - Global Script Definitions, and call the function from anywhere in the DB.

    let year := 2020;
    let week := 37;
    let day:= 4;

    let yDate := (date(year,1,1));                                "Date of the 1st day of the year as a date data type";
    let yEpoch := number(date(year,1,1));                   "same as a number - Epoch date";
    let wDay := weekday(yDate);                                "week day of the 1st day of the year as a number";
    let LydEpoch := wDay * 24 * 60 * 60 * 1000;         "Last year days in the first week - Epoch time lapse";
    let wEpoch := (week-1) * 7 * 24 * 60 * 60 * 1000; "weeks in the current year - Epoch time lapse";
    let dEpoch := day * 24 * 60* 60 * 1000;                "days in the last week - Epoch time lapse";
    date(yEpoch - LydEpoch + wEpoch + dEpoch);

    In this case you get

    11 Sep 2020

    Which is the correct date.

    You can try with other vakues for year, week, day using the Console.

    • JGM
    • Jose_Monteiro
    • 4 yrs ago
    • Reported - view

    To calculate the date for each friday in whatever year you could use a loop and call the function:

    let year := 2019;
    let day:= 4;
    for week in range(1,54) do
        let my_day := my_function(year, week, day);
        /* Since in a year you may have 366/7 = 52,28 weeks you have to loop */
        /* through 53 weeks and test my_day to see if it is after the last day in the year */
    end

    • JGM
    • Jose_Monteiro
    • 4 yrs ago
    • Reported - view

    As for the list of Mondays between two dates the above function could be used within the same year, having day := 0 and looping through week numbers in the data range.

Content aside

  • 4 yrs agoLast active
  • 13Replies
  • 4183Views