Results 1 to 4 of 4

Thread: Timer at Midnight

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    1

    Timer at Midnight

    This is a simple question. Using Timer, the time resets at midnight. I have tests I want to run for many days. How do I manipulate things so I have the total seconds from when I start over many days? I assume I will have to set some time to 0 on the start click and have a timer updating the date...but I dont know much from there. What do I do?

  2. #2

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Timer at Midnight

    Welcome to VBForums

    Another option is to store the start date & time into a variable, eg:
    Code:
    Dim datStartedAt as Date
      datStartedAt = Now
    ..and then later you can use DateDiff to find how many seconds there have been since then, eg:
    Code:
    MsgBox "Seconds since started: " & DateDiff("s", datStartedAt, Now)
    While this is simpler, it does have the disadvantage that the timing is only accurate to the nearest second.

  4. #4
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Timer at Midnight

    Welcome Aboard!
    You don't even need a timer control. Set a double precison global variable = to Now when the program commences. Then when the program ends, compare Now to the value of the global variable. You will be accuate to the nearest second, and that should be adequate accuracy for whatever you are doing.

    Here's sample code. Build a form with a label and a command button:
    Code:
    Dim StartTime As Double
    
    Private Sub Command1_Click()
    Label1.Caption = "My program ran for " & Format$(Now - StartTime, "hh:mm:ss")
    End Sub
    
    Private Sub Form_Load()
    StartTime = Now
    End Sub
    Doctor Ed

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