Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

The best calculation between 2 times


  • Please log in to reply
4 replies to this topic

#1 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 22 June 2012 - 06:26 AM

Hello, I am working on a small home project and need the very best calculation between a time interval. Now i have programmed: dtStartTime = DateTime.Now 'start 'do other things and then.... dtEndTime = DateTime.Now 'endtime 'extracting the seconds and millisecs sSeconds = (dtEndTime.TimeOfDay - dtStartTime.TimeOfDay).Seconds.ToString sMilliSeconds = (dtEndTime.TimeOfDay - dtStartTime.TimeOfDay).Milliseconds.ToString 'and put it back together dblSecsAndMsecs = CType((sSeconds + "." + sMilliSeconds), Double) My question is : Is there a better way to do this.?. Ellen

#2 Bonzo

Bonzo

    Member

  • Members
  • PipPip
  • 12 posts

Posted 22 June 2012 - 06:54 AM

I'm not going to claim that it's "better", but I THINK it is...
        Dim dtStartTime As DateTime = DateTime.Now
        'Do stuff
        Dim tsEndTime As TimeSpan = DateTime.Now.Subtract(dtStartTime)
        Debug.Print(tsEndTime.Seconds & "." & tsEndTime.Milliseconds)


#3 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 22 June 2012 - 07:09 AM

Hey Have you tried the optimised system.diagnostics.stopwatch class Chris posted here: http://forums.netdui...topwatch-class/ Its in C# but there are online converison tools like the one here: http://www.developer...t/csharp-to-vb/ Should give you greater resolution than you have now. Nak.

#4 Coding Smackdown

Coding Smackdown

    Advanced Member

  • Members
  • PipPipPip
  • 78 posts
  • LocationLewisville, TX USA

Posted 22 June 2012 - 12:21 PM

Have you tried using DateTime.Subtract? This returns a TimeSpan object that has both values that can then be used. Cheers
Brewing Award Winning Beer with a Netduino!
http://diybrewery.com

#5 Ellen

Ellen

    Advanced Member

  • Members
  • PipPipPip
  • 65 posts
  • LocationRotterdam, Netherlands

Posted 03 July 2012 - 07:31 AM

I'm not going to claim that it's "better", but I THINK it is...

        Dim dtStartTime As DateTime = DateTime.Now
        'Do stuff
        Dim tsEndTime As TimeSpan = DateTime.Now.Subtract(dtStartTime)
        Debug.Print(tsEndTime.Seconds & "." & tsEndTime.Milliseconds)


After a lot of tests, this works indead very well.
Thank you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.