Compare and Substract Date using C# .NET

Hello Guys,

Below is the function to compare date and more with date function:


       //if you want to get the countdown no. of days from starting today is
        DateTime dtToDate = Convert.ToDateTime("29/01/2012");   //end date
        DateTime dtCurrentDate = Convert.ToDateTime(DateTime.Now.ToShortDateString()); //current date (today's)

        TimeSpan ts = dtToDate - dtCurrentDate; //Days diffrence from end date to current date

        int Nodays = ts.Days;
       or you can use below code also:
      // int  Nodays = dtToDate.Substract(dtCurrentDate).days
        label1.Text = Nodays + " Days to go";
       
        //Add No of Days into date. i.e if you want to add number of days from above current date and from date diffrence into below date
        DateTime dtNextDate = Convert.ToDateTime("20/01/2012"); //Add Days
        dtNextDate = dtNextDate.AddDays(Nodays);
       

        label1.Text = "Date after " + Nodays + " Days is as: " + dtNextDate;

Comments