Wrong week number with DATE2DWY

foronavisionforonavision Member Posts: 79
I'm using DATE2DWY(date,2) to know the week number of a date and I have a problem with date 31/12/08. This is the 53rd week of year 2008 but this function returns week number 1. This function help comment shows something like this:

If the input date to the DATE2DWY function is in a week which spans two years, the DATE2DWY function computes the output year as the year of the first day in the week. This situation is shown in the example below.

It doesn't work for year 2008 but if you try with year 2009 it works.

Does anybody know the solve of this problem?
Thanks a lot!!!

Comments

  • matttraxmatttrax Member Posts: 2,309
    I think what's even more interesting is that if you use that function to return the year, you get 2009. Seems like some faulty logic to me.
    result := (mydate - CALCDATE('CY-1Y+1D', mydate)) div 7 + 1;
    

    Should give you what you're looking for.
  • fverkelfverkel Member Posts: 66
    Why do you think it is wrong?

    I'm not 100% sure, but I think this rule rules: the first Thursday of any year is in week 1.
    Don't ask me why :?:
    January 1 is a Thursday. That's in the same week as 31/12/08.
    So 31/12/08 is in week 1 of the year 2009.

    The help text seems inaccurate or incomplete.
    Keep It Simple and Stupid (KISS), but never oversimplify.
  • DenSterDenSter Member Posts: 8,305
    I always thought week 1 of any year is the week that has January 1st in it, even when it's the last day of that week.
  • kinekine Member Posts: 12,562
    I think it depends on country... and do not forgot that some countries have number 1 day in week Sunday and some Monday... ;-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Kamil is correct, in that it really does depend on the country you are in. I remember the problems we had when we first introduced Navision into Czechoslovakia.

    Some countries have 53 weeks in a year (with one short week), others have 52 (with one long week). Some start counting at the 1st of Jan, some at the first day of the week, and some consider Sunday as the first day and others start at Monday.

    So really any solution has to be country specific.

    Unfortunately Navision is "hard coded" to the Danish standard. Personally I avoid the WEEK to DATE functions because of this issue.

    The best solution is the one done in the GB version of NAV where you have multiple Accounting periods, and you can create a custom year of weeks for the country you need.
    David Singleton
  • ravi_navisionravi_navision Member Posts: 102
    Hi,

    I am having the same problem with DATE2DWY function.
    I cannot change the code right now. Because this function been used in so many places.
    With out changing code is it possible through any changes in system calendar?


    DATE2DWY(12/29/08) returns 1, should be 53


    Thanks,
    ravi
    ~~ravi
  • SavatageSavatage Member Posts: 7,142
    53 * 7 = 371 :-k

    Did i miss the addition of more days in the year? :wink:
  • DenSterDenSter Member Posts: 8,305
    DATE2DWY(12/29/08) returns 1, should be 53
    MyVariable := DATE2DWY(12/29/08);
    IF MyVariable = 1 THEN
      MyVariable := 53;
    
    Will that help? I don't see any way to change a system function's behavior, so you will have to program your own check. Mybe you will even have to write your own GetWeekNumber function.
  • ravi_navisionravi_navision Member Posts: 102
    Denster, Thanks for your reply

    I cannot do this way, Reasons:
    1. User can enter January 1st week date
    DATE2DWY(01/DD/08). Here DD can be 01|02|03|04

    DATE2DWY(12/29/08) is just one example I described.
    DATE2DWY(12/DD/08). Here DD can be 29|30|31

    2. I have to change the code many places (even I write a new function) and includes lot of testing

    I have to hardcode every where; if there is no way of changing system function's behavior

    Thanks,
    ravi
    ~~ravi
  • ajhvdbajhvdb Member Posts: 672
    Like David said; Look into the GB version or explain to your customer how NAV works.
  • DenSterDenSter Member Posts: 8,305
    I cannot do this way, Reasons:
    Look I know that my snippet will not be your entire answer, you will have to make that happen yourself. The point is that you are not happy with the way that the system calculates this value, so you will need to write your own logic. And yes you will make sure that all week number calculations in the system will point to that new function. That's just the way it is, the system works the way that the system works.

    Tell your customer how it works without a change. Then tell them how much work it will be to change it, and tell them that you may be finding areas where calculations will still be wrong because you will most likely miss a few of them. Once they know how much money it is going to be, and what kind of risk they are taking, they can make a decision whether to have you make that change or not.
Sign In or Register to comment.