Click to See Complete Forum and Search --> : Best way for duration - time pasted
invader7
November 3rd, 2009, 03:02 PM
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 DateTime.Now 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 ?
Arjay
November 3rd, 2009, 03:14 PM
Look at using TimeSpan.
Mutant_Fruit
November 3rd, 2009, 03:19 PM
DateTime prev = DateTime.Now;
...
// some time later
...
TimeSpan diff = DateTime.Now - prev;
BigEd781
November 3rd, 2009, 03:31 PM
Use a TimeSpan:
DateTime start = DateTime.Now;
...
DateTime end = DateTime.Now;
TimeSpan span = end - start;
EDIT: ninja'd :)
invader7
November 3rd, 2009, 03:34 PM
thanks all of you, thanks
invader7
November 3rd, 2009, 03:52 PM
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 ?
boudino
November 4th, 2009, 01:11 AM
There is also Stopwatch (http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx)class.
Mutant_Fruit
November 4th, 2009, 05:10 AM
"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?
invader7
November 4th, 2009, 05:52 AM
pfff my bad i understand what you mean about what it gives me... you are right.. ill solve it now thanks..
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.