Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Calculate number of month between two dates

(0) ShareShare
ReportReport
Posted on by 447

Hi 

I want to calculate the number of month between a fromDate and ToDate. 

  • Salba Z.K Profile Picture
    127 on at
    RE: Calculate number of month between two dates

    Thanks, it worked

  • Suggested answer
    YUN ZHU Profile Picture
    79,602 Super User 2025 Season 1 on at
    RE: Calculate number of month between two dates
    Hi, The calculation of the number of days only needs to be as follows.
            Day := EndDate - StartDate;
     Alternatively, you can try table 2000000007 "Date", which can also be calculated.
    Hope this helps.
    Thanks.
    ZHU
  • Salba Z.K Profile Picture
    127 on at
    RE: Calculate number of month between two dates

    How can we calculate days, as number of days are not fixed in year and month?

  • Netjacker2097 Profile Picture
    273 on at
    RE: Calculate number of month between two dates

    That working as it should, thanks for that ...had to adjust the code slightly to suite my scenario.

    Thanks..

  • Suggested answer
    NavNab Profile Picture
    4 on at
    RE: Calculate number of month between two dates

    another suggestion just in case:

    Date.Setrange("Period Type", Date."Period Type"::Month);

    Date.Setfilter("Period Start", '>=%1&<=%2', StartDate, EndDate);

    Message('Months between %1 and %2 is %3', StartDate, EndDate, Date.Count - 1);

    Variable >> Date: Record Date

  • Netjacker2097 Profile Picture
    273 on at
    RE: Calculate number of month between two dates

    Hi,

    How would you change your sample calculation to suite a service date - next service date = No. of days bitween.

    but i need another coloum to show the current no.of days left till next service.

    hope it makes sence.

  • Suggested answer
    Mehdi26 Profile Picture
    447 on at
    RE: Calculate number of month between two dates

    I did it this way:

    Clear(NoOfMonths);
    NoOfMonths := DATE2DMY("Shipment Date", 2) - DATE2DMY(TODAY, 2)   12 * (DATE2DMY("Shipment Date", 3) - DATE2DMY(TODAY, 3));

    But ZHU's answer is also correct. 

  • Verified answer
    YUN ZHU Profile Picture
    79,602 Super User 2025 Season 1 on at
    RE: Calculate number of month between two dates

    Hi, as Josh Anglesea said, you should pay attention to different years.

    A simple example: for reference only

    pageextension 50100 MyExtension extends "Customer List"
    {
        trigger OnOpenPage()
        begin
            Message('Months between %1 and %2 is %3', 20150101D, Today, CalculateMonthBetweenTwoDate(20150101D, Today));
        end;
    
        local procedure CalculateMonthBetweenTwoDate(StartDate: Date; EndDate: Date): Integer
        var
            NoOfYears: Integer;
            NoOfMonths: Integer;
        begin
            NoOfYears := DATE2DMY(EndDate, 3) - DATE2DMY(StartDate, 3);
            NoOfMonths := DATE2DMY(EndDate, 2) - DATE2DMY(StartDate, 2);
            exit(12 * NoOfYears   NoOfMonths);
        end;
    }

    pastedimage1632966783619v1.png

    Hope this will help.

    Thanks.

    ZHU

  • JAngle Profile Picture
    73 on at
    RE: Calculate number of month between two dates

    Does that assume it’s the same year?

  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    13,219 Moderator on at
    RE: Calculate number of month between two dates

    Use Date2DMY to get the both month for comparison.

    fromMonth := Date2DMY(fromDate,2);

    toMonth := Date2DMY(toDate,2);

    Diff := toMonth - fromMonth;

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,309 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,160 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans