CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2009
    Posts
    144

    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
    Code:
    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 ?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Best way for duration - time pasted

    Look at using TimeSpan.

  3. #3
    Join Date
    May 2007
    Posts
    1,546

    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.

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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

  5. #5
    Join Date
    Jun 2009
    Posts
    144

    Re: Best way for duration - time pasted

    thanks all of you, thanks

  6. #6
    Join Date
    Jun 2009
    Posts
    144

    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.

  7. #7
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  8. #8
    Join Date
    May 2007
    Posts
    1,546

    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.

  9. #9
    Join Date
    Jun 2009
    Posts
    144

    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
  •  





Click Here to Expand Forum to Full Width

Featured