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

Announcements

News and Announcements icon
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)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,306 Super User 2026 Season 1 on at

    Use Date2DMY to get the both month for comparison.

    fromMonth := Date2DMY(fromDate,2);

    toMonth := Date2DMY(toDate,2);

    Diff := toMonth - fromMonth;

  • JAngle Profile Picture
    159 on at

    Does that assume it’s the same year?

  • Verified answer
    YUN ZHU Profile Picture
    102,054 Super User 2026 Season 1 on at

    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

  • Suggested answer
    Mehdi26 Profile Picture
    453 on at

    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. 

  • Netjacker2097 Profile Picture
    286 on at

    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
    NavNab Profile Picture
    6 on at

    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

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

    Thanks..

  • Salba Z.K Profile Picture
    151 on at

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

  • Suggested answer
    YUN ZHU Profile Picture
    102,054 Super User 2026 Season 1 on at
    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

    Thanks, 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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,687 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,041 Super User 2026 Season 1

#3
Grigorios Mavrogeorgis Profile Picture

Grigorios Mavrogeorgis 974 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans