|
-
November 3rd, 2009, 04:02 PM
#1
Best way for duration - time pasted
Hello, i want to ask you which is the best way to keep duration between two events for example 2 buttons press... I want the first to get the current time and the second do the same and then calculate the duration, i managed to do this but i have some problems...
if the first button is pressed 23:00:00 (hour/minutes/sec) and the second button is pressed at 01:00:00 the calculation is wrong.... i want a way to do it for any time duration from 5 seconds to 5 years (for example)... so which is the best way ?
-
November 3rd, 2009, 04:14 PM
#2
Re: Best way for duration - time pasted
-
November 3rd, 2009, 04:19 PM
#3
Re: Best way for duration - time pasted
DateTime prev = DateTime.Now;
...
// some time later
...
TimeSpan diff = DateTime.Now - prev;
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
November 3rd, 2009, 04:31 PM
#4
Re: Best way for duration - time pasted
Use a TimeSpan:
Code:
DateTime start = DateTime.Now;
...
DateTime end = DateTime.Now;
TimeSpan span = end - start;
EDIT: ninja'd
-
November 3rd, 2009, 04:34 PM
#5
Re: Best way for duration - time pasted
thanks all of you, thanks
-
November 3rd, 2009, 04:52 PM
#6
Re: Best way for duration - time pasted
Code:
TimeSpan diff = DateTime.Now - DateTime.Parse(chargedataGridView.Rows[oRow.Index].Cells[1].Value.ToString()) ;
TimeSpan duration = new TimeSpan(diff.Hours, diff.Minutes, diff.Seconds);
chargedataGridView.Rows[oRow.Index].Cells[1].Value = duration.ToString();
i have this, it doesn't works if the first event is 23:00:00 and the second 1:00:00 and because i have it on a timer it flashes every timer tick, it shows me the very first value of cell and then the duration... all the time the same... my datagridview have add edit delete option disabled... this is the wrong ?
Last edited by invader7; November 3rd, 2009 at 05:43 PM.
-
November 4th, 2009, 02:11 AM
#7
Re: Best way for duration - time pasted
There is also Stopwatch class.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post. 
-
November 4th, 2009, 06:10 AM
#8
Re: Best way for duration - time pasted
"DateTime.Parse(chargedataGridView.Rows[oRow.Index].Cells[1].Value.ToString()) "
And what value do you expect that to give you? Did you check the output of that function call?
www.monotorrent.com For all your .NET bittorrent needs
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
-
November 4th, 2009, 06:52 AM
#9
Re: Best way for duration - time pasted
pfff my bad i understand what you mean about what it gives me... you are right.. ill solve it now thanks..
Last edited by invader7; November 4th, 2009 at 07:32 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|