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 :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Add date difference

(1) ShareShare
ReportReport
Posted on by 203

Hello Experts,

I have a requirement where I need to calculate the date difference between 2 dates

and it should be in format "2 years 9 months 8 hours"

kindly help

I have the same question (0)
  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hello Akshay,

    I am not sure if you can get the same format(mentioned by you) without code but It will definitely get you the difference between the dates in days.  Please see below links :

    community.dynamics.com/.../how-many-hours-based-on-time-date-field

    meganvwalker.com/.../

    Also, If you still want the difference in format like "2 years 9 months 8 hours" then you can write a plugin/Js to achieve the same. See link to get year,month, day in c# : https://www.codeproject.com/Articles/28837/Calculating-Duration-Between-Two-Dates-in-Years-Mo

    Hope It will help you.

  • AKSHAY AURADKAR B Profile Picture
    203 on at

    Hello Naveen Ganeshe

    Thanks for the reply

    can you send me 1 sample code if possible

  • erhan.keskin Profile Picture
    2,253 on at

    Hi,

    You can apply a method like this to calculate the date difference;

    stackoverflow.com/.../difference-in-months-between-two-dates

    Regards,

  • Suggested answer
    Naveen Ganeshe Profile Picture
    3,397 User Group Leader on at

    Hi Akshay,

    Please see the below code. It will return you the date difference in the same format you want:

    DateDifference dateDifference = new DateDifference(this.dateTimeTo.Value, this.dateTimeFrom.Value);
    string dateDifferenceResult = dateDifference.ToString();

    Implementation of above class(DateDifference.cs) :

    public class DateDifference
        {
            /// 
            /// defining Number of days in month; index 0=> january and 11=> December
            /// february contain either 28 or 29 days, that's why here value is -1
            /// which wil be calculate later.
            /// 
            private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
            /// 
            /// contain from date
            /// 
            private DateTime fromDate;
    
            /// 
            /// contain To Date
            /// 
            private DateTime toDate;
    
            /// 
            /// this three variable for output representation..
            /// 
            private int year;
            private int month;
            private int day;
    
            public DateDifference(DateTime d1, DateTime d2)
            {
                int increment;
                
                if (d1 > d2)
                {
                    this.fromDate = d2;
                    this.toDate = d1;
                }
                else
                {
                    this.fromDate = d1;
                    this.toDate = d2;
                }
    
                /// 
                /// Day Calculation
                /// 
                increment = 0;
    
                if (this.fromDate.Day > this.toDate.Day)
                {
                    increment = this.monthDay[this.fromDate.Month - 1];
    
                }
                /// if it is february month
                /// if it's to day is less then from day
                if (increment == -1)
                {
                    if (DateTime.IsLeapYear(this.fromDate.Year))
                    {
                        // leap year february contain 29 days
                        increment = 29;
                    }
                    else
                    {
                        increment = 28;
                    }
                }
                if (increment != 0)
                {
                    day = (this.toDate.Day   increment) - this.fromDate.Day;
                    increment = 1;
                }
                else
                {
                    day = this.toDate.Day - this.fromDate.Day;
                }
    
                ///
                ///month calculation
                ///
                if ((this.fromDate.Month   increment) > this.toDate.Month)
                {
                    this.month = (this.toDate.Month   12) - (this.fromDate.Month   increment);
                    increment = 1;
                }
                else
                {
                    this.month = (this.toDate.Month) - (this.fromDate.Month   increment);
                    increment = 0;
                }
    
                ///
                /// year calculation
                ///
                this.year = this.toDate.Year - (this.fromDate.Year   increment);
    
            }
    
            public override string ToString()
            {
                //return base.ToString();
                return this.year   " Year(s), "   this.month   " month(s), "   this.day   " day(s)";
            }
    
            public int Years
            {
                get
                {
                    return this.year;
                }
            }
    
            public int Months
            {
                get
                {
                    return this.month;
                }
            }
    
            public int Days
            {
                get
                {
                    return this.day;
                }
            }
    
        }

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
ManoVerse Profile Picture

ManoVerse 103 Super User 2026 Season 1

#2
11manish Profile Picture

11manish 83

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 69 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans