Measure Difference between 2 Dates


Getting differences between two given dates is as easy as it can get. The function used here is the End_Date.Subtract(Start_Date). This gives us a time span that has the time interval between the two dates. The time span can return values in the form of days, years, etc.

You can then simply display the values as Text Strings. See code below for details on how to display the results in WinForms. After this, you can see the same program in the Console Environment. 

namespace Date_Difference_in_CSharp

{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void dddtpStart_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate = (DateTime)dddtpStart.Value;
                DateTime endDate = (DateTime)dddtpEnd.Value;
                TimeSpan ts = endDate.Subtract(startDate);

                ddtxtDays.Text = ts.Days.ToString();
                ddtxtDifference.Text = (Convert.ToDouble(ddtxtDays.Text) / 365).ToString();
            }
            catch
            {
            }
        }

        private void dddtpEnd_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                DateTime startDate = (DateTime)dddtpStart.Value;
                DateTime endDate = (DateTime)dddtpEnd.Value;
                TimeSpan ts = endDate.Subtract(startDate);

                ddtxtDays.Text = ts.Days.ToString();
                ddtxtDifference.Text = (Convert.ToDouble(ddtxtDays.Text) / 365).ToString();
            }
            catch
            {
            }
        }
    }
}

Below, you can see a program written in the C# Console environment that does the same work:


using System;
using System.Text;

namespace Print_and_Get_Date_Difference_in_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("*******************************************");
            Console.WriteLine("********www.code-kings.blogspot.com********");
            Console.WriteLine("*******************************************\n");
            Console.WriteLine("The Date Today Is:");
            DateTime DT = new DateTime();
            DT = DateTime.Today.Date;
            Console.WriteLine(DT.Date.ToString());

            Console.WriteLine("Calculate the difference between two dates:\n");
            int year, month, day;
           
            Console.WriteLine("Enter Start Date");
            Console.WriteLine("Enter Year");
            year = Convert.ToInt16(Console.ReadLine().ToString());
            Console.WriteLine("Enter Month");
            month = Convert.ToInt16(Console.ReadLine().ToString());
            Console.WriteLine("Enter Day");
            day = Convert.ToInt16(Console.ReadLine().ToString());
            DateTime DT_Start = new DateTime(year,month,day);

            Console.WriteLine("\nEnter End Date");
            Console.WriteLine("Enter Year");
            year = Convert.ToInt16(Console.ReadLine().ToString());
            Console.WriteLine("Enter Month");
            month = Convert.ToInt16(Console.ReadLine().ToString());
            Console.WriteLine("Enter Day");
            day = Convert.ToInt16(Console.ReadLine().ToString());
            DateTime DT_End = new DateTime(year, month, day);

            TimeSpan timespan = DT_End.Subtract(DT_Start);
            Console.WriteLine("\nThe Difference is:\n");
            Console.WriteLine(timespan.TotalDays.ToString() + " Days\n");
            Console.WriteLine((timespan.TotalDays / 365).ToString() + " Years");

            Console.ReadKey();
        }
    }
}