web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Date Functions

(0) ShareShare
ReportReport
Posted on by 249

This a SQL Script, it helps me get the months between two dates, how can i achieve the same in AL Development (D365 Business Central)

datediff(month,'Specified Date',getdate())

Categories:
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    In AL, numdays := Date1 - Date2;

  • Verified answer
    Community Member Profile Picture
    on at

    Hi Dickson,

    Well, to get the difference in months between two dates I suggest you to create this function:

    Procedure getDatesDiffInMonths(firstDate : Date; secondDate : Date) monthsDiff : Integer
    begin
    
        monthsDiff := ABS(12 * (DATE2DMY(firstDate, 3) - DATE2DMY(secondDate, 3)) + DATE2DMY(firstDate, 2) - DATE2DMY(secondDate, 2));
    
    end;

    then you call it in your code :

    diffInMonths := getDatesDiffInMonths(TodayDate, AnotherPastDate);

    Note that DATE2DMY(firstDate, 3) returns the Year and DATE2DMY(firstDate, 2) returns the Month of the date in parameter (firstDate).

    --

    Don't hesitate If you need more details or explanations.

    -------------

    Please verify my answer if you find my answer helpful. Doing so you'll show other community members that there was found a solution and you credit my help.

    -------------

    Best Regards,

  • dickson kimeu Profile Picture
    249 on at

    Thank you.

    It worked.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans