Shaitan00 Posted August 21, 2005 Posted August 21, 2005 Simply put - I am making a program that allows you to START, PAUSE, and STOP a timer (this timer is to calculate how much time I spend on a certain work project - but that shouldn't matter) I need to save the START Timestamp (date/time), STOP Timestamp (date/time), and ELASPED Time (time). Problem is I don't know the best way to determine the ELAPSED time?... (note that I use an EXCEL file to store my information - like a Database of somekind) Meaning, the user can start a task today, do it for 3 days, pause it for 2 weeks, start again for a few hours, then STOP it. How do I display the elapsed time? (getting the START and STOP timestamps are easy - they will be NOW() of when the START/STOP buttons are pressed). I mean - I could use a temp variable to hold the new_start_time (NOW()) of when the task Starts, even after a pause) and when I pause/stop I just do NOW() - new_start_time and add that value to whatever current exists in ELAPSED TIME? Is that a good approach? Just looking for some advice/guidance/assurance that I have a valid method - or to learn a new/better one, hope I spelled this out right. Thanks, Quote
Diesel Posted August 21, 2005 Posted August 21, 2005 As long as you have 2 DateTime objects, the DateTime class has an overloaded subtraction operator that retruns a TimeSpan object. Therefore to get the time difference: DateTime start = DateTime.Now; DateTime end = DateTime.Now.AddHours(3); TimeSpan elapsed = end - start; MessageBox.Show("Days: " + elapsed.Days.ToString() + " Hours: " + elapsed.Hours.ToString() + " Minutes: " + elapsed.Minutes.ToString() ); [/Code] Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.