Name

Timer Property

Class

Microsoft.VisualBasic.DateAndTime

Syntax

Timer

Return Value

Double representing the number of seconds that have elapsed since midnight

Description

Returns the number of seconds since midnight

Programming Tips and Gotchas

  • Timer is classified as a function in VB 6 and as a read-only property in VB.NET.

  • You can use the Timer property as an easy method of passing a seed number to the Randomize procedure, as follows:

    Randomize Timer(  )
  • The Timer property is ideal for measuring the time taken to execute a procedure or program statement, as the following snippet shows:

    Dim sStartTime As Single
    Dim i As Integer
        
    sStartTime = Timer(  )
        For i = 1 To 100
            Console.WriteLine("Hello")
        Next i
    MsgBox("Time Taken = " & Timer(  ) - sStartTime & " Seconds")

VB.NET/VB 6 Differences

While the Timer property returns a Double in VB.NET, the VB 6 Timer function returns a Single.

Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.