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 ?
Re: Best way for duration - time pasted
Re: Best way for duration - time pasted
DateTime prev = DateTime.Now;
...
// some time later
...
TimeSpan diff = DateTime.Now - prev;
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 :)
Re: Best way for duration - time pasted
thanks all of you, thanks
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 ?
Re: Best way for duration - time pasted
There is also Stopwatch class.
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?
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..