Difference or Gap of days between two given dates using C#

In this tutorial, you will be learning how to find the difference or gap between two dates in C#.

The user will enter two dates in the form of DD\MM\YYYY. The total number(gap or difference) of days between two date is an output.

Design:

There are many approaches to solve this problem:

  1. The first approach is to find the number of days from the date d1 to date d2.
  2. The second approach is to find the number of days(nd1) before the date d1 i.e., from 00/00/0000 and number of days(nd2) before the date d2. The difference between the two days(nd2-nd1) obtained is the actual gap between the two dates.

We will implement the second approach as it is straight forward and easy to understand.

Example:

Let the two dates be:
d1 = 2/2/2018
d2=1/2/2019

Count number of days before d1. Let this count be nd1. The leap year contains an extra day that has to be added to nd1.

nd1 = year*365+month[i]+dd+number of leap year.
where 1<=i<mon and month[] is array of no of days in each month

Therefore nd1=2018*365+(31)+2+number of leap years.

To calculate the number of leap years:

Count of leap year for a date ‘dd/mm/yyyy’ can be calculated using the following formula:

  1. Number of leap years = y/4 – y/100 + y/400 if m > 2
  2. Number of leap years= (y-1)/4 – (y-1)/100 + (y-1)/400 if m <= 2

(All above divisions must be done using integer arithmetic so that the remainder is ignored)

for date 2/2/2018, the number of leap year is:

Number of leap years=2017/4-2017/100+2017/400=489  (using 2nd formula as m<=2)

Therefore, nd1=2018*365+(31)+2+489=737092

Similarly, find the number of days before date2, i.e nd2=737456

Therefore, the number of days between date1 and date2 is (nd2-nd1) 364 days.

Difference between two dates in C#

Below shows the program to find the number of days between two dates in C# using the above approach.

// C# program two find number of  days between two given dates 
using System; 
  
class pgm 
{ 
 
    public class Date  
    { 
        public int day, mon, year;
  
    }; 
    static void get_date(Date d)
    {
       	Console.WriteLine("Enter day:");
    	d.day=int.Parse(Console.ReadLine());
    	Console.WriteLine("Enter month:");
    	d.mon=int.Parse(Console.ReadLine());
    	Console.WriteLine("Enter year:");
    	d.year=int.Parse(Console.ReadLine());
    }
 
    static int []monthDays = {31, 28, 31, 30, 31, 30, 
                            31, 31, 30, 31, 30, 31}; 
  
    // This function counts number of  
    // leap years before the given date 
    static int countLeapYears(Date d)  
    { 
        int years = d.year; 
  
        // Check if the current year  
        // needs to be considered 
        // for the count of leap years or not 
        if (d.mon <= 2)  
        { 
            years--; 
        } 
        return years / 4 - years / 100 + years / 400; 
    } 
  
    // This function returns number  
    // of days between two given dates 
    static int getDifference(Date dt1, Date dt2) 
    { 
        // COUNT TOTAL NUMBER OF DAYS  
        // BEFORE FIRST DATE 'dt1' 
  
        // initialize count using years and day 
        int nday1 = dt1.year * 365 + dt1.day; 
  
        // Add days for months in given date 
        for (int i = 0; i < dt1.mon- 1; i++)  
        { 
            nday1 += monthDays[i]; 
        } 
  
        // Since every leap year is of 366 days, 
        // Add a day for every leap year 
        nday1 += countLeapYears(dt1); 
  
        // SIMILARLY, COUNT TOTAL  
        // NUMBER OF DAYS BEFORE 'dt2' 
        int nday2 = dt2.year * 365 + dt2.day; 
        for (int i = 0; i < dt2.mon - 1; i++) 
        { 
            nday2 += monthDays[i]; 
        } 
        nday2 += countLeapYears(dt2); 
  
        // return difference between two counts 
        return (nday2 - nday1); 
    } 
    public static void Main(String[] args)  
    { 
        Date dt1 = new Date(); 
        Date dt2 = new Date(); 
    	Console.WriteLine("Enter the date in the pattern i.e  dd/mm/yy");
    	Console.WriteLine(" First Date :");
    	get_date(dt1);
    	Console.WriteLine(" Second Date :");
    	get_date(dt2);
    	Console.WriteLine("******First Date******");
        Console.WriteLine(dt1.day+"\\"+dt1.mon+"\\"+dt1.year) ;
    	Console.WriteLine("******Second Date*****");
    	Console.WriteLine(dt2.day+"\\"+dt2.mon+"\\"+dt2.year) ;
        Console.WriteLine("******Result*****");
    	Console.WriteLine("Difference between two dates is " + Math.Abs( getDifference(dt1, dt2))+" days"); 
    } 
}  
  
// This code is contributed by DEVIPRASAD D MAHALE 

Output:

Enter the date in the pattern i.e dd/mm/yyyy
First Date 
Enter the date:2
Enter the month:2
Enter the date:2018
Second Date:
Enter the date:1
Enter the month:2
Enter the date:2019
********** First Date **********
Date is 2/2/2018
********** Second Date **********
Date is 1/2/2019
********** Result **********
Difference between two dates is 364 days.

Hope you have understood the above program to find the difference or gap between two given dates in C#.

Leave a Reply

Your email address will not be published. Required fields are marked *