DATEPART In C/AL

ricky76ricky76 Member Posts: 204
edited 2006-08-01 in Navision Attain
Is there any way to return the datepart in CAL similar to how it can be done in SQL. I need to put something together that provides information on the earlier part of the week but need to somehow recognise from the date the user puts in what day of the week it is eg. If it is a Thursday i need to populate information for Monday, Tuesday and Wednesday. Is this too ambitious or would i be better to write it in SQL and import it into a Navision table?? Any help would be appreciated.

Comments

  • krikikriki Member, Moderator Posts: 9,112
    If I understood well, you have a day in the week and you need the date of its Monday?
    recDate.RESET;
    recDate.SETCURRENTKEY("Date Type","Period Start");
    recDate.SETRANGE("Date Type",recDate.Date Type"::Week);
    recDate.SETRANGE("Period Start",0d,datTheDate);
    recDate.FIND('+');
    MESSAGE('Monday has date %1',recDate."Period Start");
    MESSAGE('Tuesday has date %1',recDate."Period Start" + 1);
    ...
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ricky76ricky76 Member Posts: 204
    Sorry may still need some help here. Yes i do want to know a date's Monday. The user will enter a date in the FromDate text box. So if the user puts in 21/07/06 how do i return the Monday date of 17/07/06?
  • krikikriki Member, Moderator Posts: 9,112
    In the OnValidate-trigger (or OnAfterValidate):
    recDate.RESET;
    recDate.SETCURRENTKEY("Date Type","Period Start");
    recDate.SETRANGE("Date Type",recDate.Date Type"::Week);
    recDate.SETRANGE("Period Start",0d,datFromDate);
    recDate.FIND('+');
    datFromDate := recDate."Period Start";
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ricky76ricky76 Member Posts: 204
    Sorry may still need some help here. Yes i do want to know a date's Monday. The user will enter a date in the FromDate text box. So if the user puts in 21/07/06 how do i return the Monday date of 17/07/06?
  • ricky76ricky76 Member Posts: 204
    I am trying to do this in a report. I want to put all the data in a temp table and report over that so i am doing all my coding in OnPreDataItem
  • krikikriki Member, Moderator Posts: 9,112
    ricky76 wrote:
    Sorry may still need some help here. Yes i do want to know a date's Monday. The user will enter a date in the FromDate text box. So if the user puts in 21/07/06 how do i return the Monday date of 17/07/06?
    After the codepart I posted in the last post, the date of the Monday is in recDate."Period Start".

    BTW : "recDate" is a table on virtual table "Date".
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • ricky76ricky76 Member Posts: 204
    I'm getting closer. My only problem is the 2 lines

    recDate.SETCURRENTKEY("Date Type","Period Start");
    recDate.SETRANGE("Date Type",recDate.Date Type"::Week);

    I return an error saying i have specified an unknown variable "Date Type". I ran the table Date to see what fields it has in it but "Date Type" is not there. I'm not sure you mean that "Date Type" is a field but i am stuck. At the moment if i put in 21/07/07 i get the message Monday has date 01/01/06. I do really appreciate your help.
  • krikikriki Member, Moderator Posts: 9,112
    The table is no. 2000000007. Is it this one that you use?
    There are some fields:
    "Period Type" : option (the first field of the primary key)
    "Period Start" : date (the second field of the primary key)
    "Period End" : date
    "Period No." : integer
    "Period Name" : text
    It is possible the table and the fields have been translated.
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • kinekine Member Posts: 12,562
    Do not forget about the calcdate function!
      MondayDate := CALCDATE('<-CW>',UserDate);
    

    Is solution for you!
    (CW = current week, <> is used to have it multilanguage enabled)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • ricky76ricky76 Member Posts: 204
    Thanks for your help on this guys i have it working perfectly.
Sign In or Register to comment.