.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Imran Ghani

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to find elapsed time between two DateTime

Posted By: priya     Posted Date: December 20, 2009    Points:2   Category :ASP.Net

How to find elapsed time between two DateTime? My requirement is to get the datetime difference two given input dates.

For example

result: 3 days 5 minutes ago


Author: Lavanya             
Posted Date: December 22, 2009     Points: 5   

int months, years, days, weeks;
bool leapyear;
float minutes;
int hours;
DateTime dt1 = Convert.ToDateTime(textBox1.Text);
DateTime dt2 = Convert.ToDateTime(textBox2.Text);

TimeSpan dt;
if (dt1 < dt2)
dt = dt2 - dt1;
months = dt2.Month - dt1.Month;
years = dt2.Year - dt1.Year;
months = months + years * 12;

dt = dt1 - dt2;
months = dt1.Month - dt2.Month;
years = dt1.Year - dt2.Year;
months = months + years * 12;

minutes = dt.Minutes;
hours = dt.Hours;
days = dt.Days;
weeks = dt.Days / 7;

You can find minutes,days,weeks,hours,monhts using timespan

Author: nishithraj             
Posted Date: December 23, 2009     Points: 5   

int days = ((TimeSpan)(endDate-startDate)).Days;

Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend