Bug in CALCDATE?

helmhelm Member Posts: 46
edited 2007-04-25 in Navision Attain
Hello,

I'm fairly new to C/SIDE and don't know if I've made the right conclusions. I seems to me that there is a bug in CALCDATE. The code i've tried is CALCDATE('<CM-1M+1D>',WORKDATE). I'm trying to get the first date of the month that includes the current workdate. If Workdate is in February it returns 070129. If it's in April the returnvalue is 070331. Works for January and Mars though. It always withdraws 31 days. Also tried the following code for testing CALCDATE('<CM+1M>',WORKDATE). It returned for January 070228 (correct), february 070328 (wrong), Mars 070430 (correct) and April 070530 (wrong). Haven´t tried it for the rest of the year. BTW the dateformat in sweden is YYMMDD.

Am I missunderstanding something about CALCDATE or is it a bug? ](*,). If it´s a bug, does anybody know of a simple workaround?

Thanks in advance.

Answers

  • krikikriki Member, Moderator Posts: 9,112
    Your explanation is a little confused: do you want the first day of current month or the last date of current month or the last date of next month?
    To be sure, I give all 3 possibilities:

    datTheDate := 24042007D;

    First day of current month:
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)));
    

    Last date of current month:
    datTheDate := calcdate('<+1M>',datTheDate);
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)) - 1);
    

    Last date of next month:
    datTheDate := calcdate('<+2M>',datTheDate);
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)) - 1);
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • David_SingletonDavid_Singleton Member Posts: 5,479
    helm wrote:
    Hello,

    I'm fairly new to C/SIDE and don't know if I've made the right conclusions. I seems to me that there is a bug in CALCDATE. The code i've tried is CALCDATE('<CM-1M+1D>',WORKDATE). I'm trying to get the first date of the month that includes the current workdate. If Workdate is in February it returns 070129. If it's in April the returnvalue is 070331. Works for January and Mars though. It always withdraws 31 days. Also tried the following code for testing CALCDATE('<CM+1M>',WORKDATE). It returned for January 070228 (correct), february 070328 (wrong), Mars 070430 (correct) and April 070530 (wrong). Haven´t tried it for the rest of the year. BTW the dateformat in sweden is YYMMDD.

    Am I missunderstanding something about CALCDATE or is it a bug? ](*,). If it´s a bug, does anybody know of a simple workaround?

    Thanks in advance.

    Its not a bug, you are missing something. Try

    CALCDATE('<-CM>',WORKDATE)
    David Singleton
  • DenSterDenSter Member Posts: 8,307
    That's a good one, calling a code feature a bug because it doesn't do what you think it does :mrgreen: :bug:
  • kinekine Member Posts: 12,562
    I need to correct this:
    kriki wrote:
    datTheDate := 24042007D;

    First day of current month:
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)));
    

    Last date of current month:
    datTheDate := calcdate('<+1M>',datTheDate);
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)) - 1);
    

    Last date of next month:
    datTheDate := calcdate('<+2M>',datTheDate);
    MESSAGE('<%1>',dmy2date(1,date2dmy(datTheDate,2),date2dmy(datTheDate,3)) - 1);
    

    Correct using of CALCDATE:
    First day of current month (as David wrote):
    datTheDate := calcdate('<-CM>',datTheDate);
    MESSAGE('<%1>',datTheDate);
    

    Last date of current month:
    datTheDate := calcdate('<CM>',datTheDate);
    MESSAGE('<%1>',datTheDate);
    

    Last date of next month:
    datTheDate := calcdate('<CM+1M>',datTheDate);
    MESSAGE('<%1>',datTheDate);
    

    :-) It is easy. You need to know that if you do not enter +/-, + is used automatically.
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • helmhelm Member Posts: 46
    Correct using of CALCDATE:
    First day of current month (as David wrote):

    Code:

    datTheDate := calcdate('<-CM>',datTheDate);
    MESSAGE('<%1>',datTheDate);


    Last date of current month:
    Code:
    datTheDate := calcdate('<CM>',datTheDate);
    MESSAGE('<%1>',datTheDate);


    Last date of next month:
    Code:
    datTheDate := calcdate('<CM+1M>',datTheDate);
    MESSAGE('<%1>',datTheDate);


    It is easy. You need to know that if you do not enter +/-, + is used automatically.

    Thanks for all replies. I've tested the ones above and the first two worked just fine, using the workdate. (The first one was the one I wanted). But eager to learn as I am I tested all three. The last one works just fine for January and Mars. However, if workdate is in february it returns 070328, and if it's april it returns 070530. Same result (and same code) as in my previous attempt. Since my main problem is solved :D it's not important, but I would like to know if it's supposed to work like this. :-s
  • kinekine Member Posts: 12,562
    Ok, than you can try it with <1M+CM> instead <CM+1M>. 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • helmhelm Member Posts: 46
    Thanks, that one worked.
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    ...
    Correct using of CALCDATE:
    First day of current month (as David wrote):

    [code]
    Last date of next month:
    [code]datTheDate := calcdate('<CM+1M>',datTheDate);
    MESSAGE('<%1>',datTheDate);[/code]

    Actually this is probably what Helm was thinking.

    But +1m, actually finds the same date in the next month. So if you are in a month with 31 days it works, since the next month does not have 31 days, it rolls back to 28,29 or 30. But if you do this say in April, then CM give you April 30, +1m gives you May 30, where as you really want May 31, so the correct code is

    CALCDATE('<1m+CM>',WORKDATE)
    David Singleton
  • David_SingletonDavid_Singleton Member Posts: 5,479
    kine wrote:
    Ok, than you can try it with <1M+CM> instead <CM+1M>. 8)

    Opps sorry, I obviously had this open a long time. Still I am sure my explanation helps.
    David Singleton
  • helmhelm Member Posts: 46
    Thanks David, now I don't only know that it works but why and how it works. Another thanks to Kine, who posted the first solution
  • David_SingletonDavid_Singleton Member Posts: 5,479
    helm wrote:
    Thanks David, now I don't only know that it works but why and how it works. Another thanks to Kine, who posted the first solution

    Actually when I first started in Navision (back in DOS days) I wrote a form that I used to learn CALCDATE.

    I converted it to Windows some time ago to help explain CALCDATE to a client.

    I have uploaded it on my site here:

    http://www.go-live.us/downloads.htm

    Feel free to use it for training purposes.
    David Singleton
  • helmhelm Member Posts: 46
    Thanks David. Sadly none of the links under "Understanding the CALCDATE function" worked :( . Got an 404 page not found-error
  • David_SingletonDavid_Singleton Member Posts: 5,479
    helm wrote:
    Thanks David. Sadly none of the links under "Understanding the CALCDATE function" worked :( . Got an 404 page not found-error

    You were two fast, I was uploading whilst you were trying to down load.
    David Singleton
Sign In or Register to comment.