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


Top 5 Contributors of the Month
david stephan

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

Calculate running time

Posted By: Suma     Posted Date: May 20, 2009    Points:2   Category :ASP.Net
Hi,
I need to display running time in terms of days, hours and minutes. for eg.

if Birth date is 1/1/2009 10:00 and now the date and time is 1/5/2009 the i need to get the time like this

118 days 10 hours and 45 min.

I am using C# and asp.net.

Please help me.

Responses
Author: Amit Mehra             
Posted Date: May 22, 2009     Points: 5   

HI Suma,
To calculate the running time you can take the TimeSpan and get the Difference from the current date and the input date and from the Timespan you can get the Date and time difference of the Time Sapan properties.

For eg

private void calculateTime()
{
try
{
DateTime dt1 = DateTime.Now;
DateTime dt2 = DateTime.Now.AddDays(-7);
TimeSpan ts = dt1.Subtract(dt2);
int totalsec = ts.Days;
Response.Write(ts.Days +" Days");
Response.Write(" " + ts.Hours+" Hours");
Response.Write(" " + ts.Minutes+" Minutes ago");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}


Thanks,
Amit
Please "Mark As Answer " if this post help you.


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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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