Click here to Skip to main content
14,425,302 members

Calculate Difference Between Two Dates in Day, Week, Month, and Year Parts

Rate this:
4.54 (18 votes)
Please Sign up or sign in to vote.
4.54 (18 votes)
18 Dec 2013CPOL
Calculate difference between two dates in day, week, month, and year parts.


During my investigation of the DateTime and TimeSpan classes, I faced the following question several times, and unfortunately, I didn't find an answer to it.

How can I get the difference between two dates in number of days, weeks, months, and years? I tried to use the Subtract method of the DateTime class, and it gave me the difference in TimeSpan, from which I can get the duration in days, hours, and so on... but how can I get the difference in larger date parts?

I found it a very common requirement to display the date difference between two dates with high precision, considering leap years and the number of days in each month.

So I decided to write my own library (DateCalculator_Lib) which provides to its user a complete set of properties for all required date parts (days, weeks, months, years) along with some extra properties like number of leap years and a collection containing the accrual of each month during the difference period, and a group of properties representing each date part total accrual in the difference period, like total number of days, months,......etc.

Using the Code

All you need is to add a reference to DateCalculator.dll in your .NET project and the using statement for the DateCalculator namespace, then create an object from the DateCalculator class which will need the start and end dates in its constructor.

After that, just call the public CalculateDateDifference method from your object, and it will calculate each date difference part and assign it to the appropriate property.

Use those read only properties to retrieve all your needs....

The attached demo project contains all the previously explained steps.

//Add the using statement 
using DateCalculator;

private void button1_Click(object sender, EventArgs e) 
    //create the object 
    DateCalculator.DateCalculator DC = new DateCalculator.DateCalculator
    ( this.dateTimePicker1.Value, this.dateTimePicker2.Value); 

    //call the method 
    // use the read only properties
    this.day_txt.Text = DC.Days.ToString(); 
    this.week_txt.Text = DC.Weeks.ToString(); 
    this.month_txt.Text = DC.Months.ToString(); 
    this.year_txt.Text = DC.Years.ToString(); 

Algorithm Explanation

This library depends on a well known algorithm called "Goal Seek" algorithm; we start from the "Start Date" and go on until we reach the "End Date", which is our goal...during the journey, we collect information by accumulating days and months and leap years inside the period between the start and end dates.

The algorithm depends on a container "Integer array" that contains the number of days in each month except February, as this month is the differentiator between normal and leap years.

And the algorithm logical steps are as follows:

  1. If our Goal -->>"End date" is not reached, continue.
  2. Check if current month is February or not, and accumulate days and months.
  3. Update the public properties.
  4. Check the termination condition "Did we reach the goal or not".
  5. If not, get the next month index and continue in the loop.
  6. If we exceed the goal, we seek it by going forward and backward until we stop on the end date.

Finally, after finishing this loop, all public properties will contain the accumulated information collected through the cycle.


  • 19th November, 2009: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Mahmoud Fathy Afify
Software Developer International TurnKey Systems (ITS)
Egypt Egypt
our work represents us

Comments and Discussions

GeneralMy vote of 1 Pin
Kamran Behzad19-Jan-15 17:11
MemberKamran Behzad19-Jan-15 17:11 
QuestionUnable to download Files. Get blank Page. Pin
Len202021-Feb-14 16:11
MemberLen202021-Feb-14 16:11 
AnswerRe: Unable to download Files. Get blank Page. Pin
Nick203030-Apr-14 16:50
MemberNick203030-Apr-14 16:50 
GeneralRe: Unable to download Files. Get blank Page. Pin
Len202030-Apr-14 16:56
MemberLen202030-Apr-14 16:56 
GeneralRe: Unable to download Files. Get blank Page. Pin
Nick203030-Apr-14 17:03
MemberNick203030-Apr-14 17:03 
QuestionDays in minus (-) Pin
hdamis17-Feb-14 2:17
Memberhdamis17-Feb-14 2:17 
GeneralFirst and Last Date. Pin
ZamirF18-Dec-13 8:51
MemberZamirF18-Dec-13 8:51 
Bugwrong result returned when from date is 10/10/2004 and to date is 28/02/2005 Pin
Vasudevan Kannan2-Jan-12 14:45
MemberVasudevan Kannan2-Jan-12 14:45 
GeneralRe: wrong result returned when from date is 10/10/2004 and to date is 28/02/2005 Pin
Mahmoud Fathy Afify18-Dec-13 3:09
MemberMahmoud Fathy Afify18-Dec-13 3:09 
GeneralRe: wrong result returned when from date is 10/10/2004 and to date is 28/02/2005 Pin
Mahmoud Fathy Afify18-Dec-13 4:21
MemberMahmoud Fathy Afify18-Dec-13 4:21 
GeneralRe: wrong result returned when from date is 10/10/2004 and to date is 28/02/2005 Pin
Vasudevan Kannan30-Dec-13 21:25
MemberVasudevan Kannan30-Dec-13 21:25 
Generalerror I think Pin
galache23-Dec-10 7:20
Membergalache23-Dec-10 7:20 
GeneralRe: error I think Pin
Gustav Brock23-Dec-13 4:03
professionalGustav Brock23-Dec-13 4:03 
GeneralWhy not... Pin
n0pt3x20-Nov-09 3:19
Membern0pt3x20-Nov-09 3:19 
GeneralRe: Why not... Pin
Mahmoud Fathy Afify21-Nov-09 23:00
MemberMahmoud Fathy Afify21-Nov-09 23:00 
GeneralRe: Why not... Pin
bkyee19-Dec-13 0:23
professionalbkyee19-Dec-13 0:23 
GeneralRe: Why not... Pin
Gustav Brock23-Dec-13 3:59
professionalGustav Brock23-Dec-13 3:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 19 Nov 2009

Tagged as


46 bookmarked