Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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,

Posted

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]

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...