web
You’re offline. This is a read only version of the page.
close
Skip to main content
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 453

Hi 

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

I have the same question (0)
  • Salba Z.K Profile Picture
    151 on at
    RE: Calculate number of month between two dates

    Thanks, it worked

  • Suggested answer
    YUN ZHU Profile Picture
    90,553 Super User 2025 Season 2 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
    151 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
    286 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
    6 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
    286 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
    453 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
    90,553 Super User 2025 Season 2 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
    113 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
    14,064 Super User 2025 Season 2 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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
Sumit Singh Profile Picture

Sumit Singh 2,856

#2
Sohail Ahmed Profile Picture

Sohail Ahmed 2,681 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 2,342 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans