Thursday, June 16, 2011

How to calculate difference between two date in ASP.NET with C#


         TimeSpan tspan;
         DateTime date1 = Convert.ToDateTime(txtDateFrom.Text);
         DateTime date2 = Convert.ToDateTime(txtDateTo.Text);
         if (date1 > date2)
             tspan = date1.Subtract(date2);
         else
             tspan = date2.Subtract(date1);
         int days = Convert.ToInt32(tspan.TotalDays);

0 comments:

Post a Comment