First Day of the Month

slmaluwaslmaluwa Member Posts: 358
Hi
Say, DateX is a date variable holding some unknown date.

How do I find the first day and the last day of the month in that date variable?

TIA

Maluwa
"A bove maiore discit arare minor"-"From the old ox, the young one learns to plow."

Answers

  • krikikriki Member, Moderator Posts: 9,094
    datFirstDayOfMonth := DMY2DATE(1,DATE2DMY(datSomeDate,2),DATE2DMY(datSomeDate,3)));
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • DenSterDenSter Member Posts: 8,304
    I believe the last day of the month is doing a date calculation with 'CM' as the dateformula. Not sure though, I'd have to verify that.
  • kinekine Member Posts: 12,562
    First day of month is
    FirstDayDate := CALCDATE('<-CM>',MyDate)l
    

    And that's all..
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,094
    kriki wrote:
    datFirstDayOfMonth := DMY2DATE(1,DATE2DMY(datSomeDate,2),DATE2DMY(datSomeDate,3)));
    

    Did see that also the last day of the month was needed:
    datLastDayOfMonth := CALCDATE('<1M>',datFirstDayOfMonth) - 1;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • kinekine Member Posts: 12,562
    kriki wrote:
    kriki wrote:
    datFirstDayOfMonth := DMY2DATE(1,DATE2DMY(datSomeDate,2),DATE2DMY(datSomeDate,3)));
    

    Did see that also the last day of the month was needed:
    datLastDayOfMonth := CALCDATE('<1M>',datFirstDayOfMonth) - 1;
    

    Or in generaly:

    datLastDayOfMonth := CALCDATE('<CM>',datMyDate);
    

    :wink: 8)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • krikikriki Member, Moderator Posts: 9,094
    kine wrote:
    kriki wrote:
    kriki wrote:
    datFirstDayOfMonth := DMY2DATE(1,DATE2DMY(datSomeDate,2),DATE2DMY(datSomeDate,3)));
    

    Did see that also the last day of the month was needed:
    datLastDayOfMonth := CALCDATE('<1M>',datFirstDayOfMonth) - 1;
    

    Or in generaly:

    datLastDayOfMonth := CALCDATE('<CM>',datMyDate);
    

    :wink: 8)
    And with this we are proving there are, most of time, multiple good solutions for the same problem. :wink: 8)
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • garakgarak Member Posts: 3,263
    and the last solution, but to many code lines, go over the Day record :lol:
    Do you make it right, it works too!
  • kopavkopav Member Posts: 1
    edited 2022-07-01
    Please pay attention to use '<CM>', not just 'CM'. 'CM' will throw an error if language is not English))
  • JJMcJJMc Member Posts: 59
    Or just DMY2Date(1);
Sign In or Register to comment.