You are here:
Visual
Basic > VB6
(Beginners Tutorial)
Previous Page | Table
of Contents | Next Page
Getting and Setting the Current Date and Time
Strictly speaking, Date and Time aren't functions: They're properties. In fact,
you can use them to either retrieve the current date and time (as Date values)
or assign new values to them to modify the system settings:
Print Date & " " & Time ' Displays "8/14/98 8:35:48
P.M.".
' Set a new system date using any valid date format.
Date = "10/14/98"
Date = "October 14, 1998"
To help you compare the outcome of all date and time functions, all the examples
in this section assume that they're executed at the date and time shown in the
preceding code snippet: October 17, 2008, 9:25:33 p.m.
The outdated Date$ and Time$ properties can also be used for the same task.
They're String properties, however, and therefore recognize only the mm/dd/yy
or mm/dd/yyyy formats and the hh:mm:ss and hh:mm formats, respectively. For this
reason, it's usually better to use the new $-less functions.
The Now function returns a Date value that contains the current date and time:
Print Now ' Displays "10/17/2008 9:25:33 P.M.".
But the time-honored Timer function returns the number of seconds elapsed from
midnight and is more accurate than Now because the Timer function includes fractional
parts of seconds. (The actual accuracy depends on the system.) This function is
often used for benchmarking a portion of code:
StartTime = Timer
' Insert the code to be benchmarked here.
Print Timer - StartTime
The preceding code suffers from some inaccuracy: The StartTime variable might
be assigned when the system tick is about to expire, so your routine could appear
to take longer than it actually does. Here's a slightly better approach:
StartTime = NextTimerTick
' Insert the code to be benchmarked here.
Print Timer _ StartTime
' Wait for the current timer tick to elapse.
Function NextTimerTick() As Single
Dim t As Single
t = Timer
Do: Loop While t = Timer
NextTimerTick = Timer
End Function
If you're using the Timer function in production code, you should be aware
that it's reset at midnight, so you always run the risk of introducing unlikely
but potentially serious errors. Try to spot the bug in this routine, which adds
a CPU-independent pause in your code:
' WARNING: this procedure has a bug.
Sub BuggedPause(seconds As Integer)
Dim start As Single
start = Timer
Do: Loop Until Timer _ start >= seconds
End Sub
The bug manifests itself very rarely—for example, if the program asks
for a 2-second pause at 11:59:59 p.m. Even if this probability is small, the effect
of this minor bug is devastating and you'll have to press Ctrl+Alt+Del to kill
your compiled application. Here's a way to work around this issue:
' The correct version of the procedure
Sub Pause(seconds As Integer)
Const SECS_INDAY = 24! * 60 * 60 ' Seconds per day
Dim start As Single
start = Timer
Do: Loop Until (Timer + SECS_INDAY - start) Mod SECS_INDAY >= seconds
End Sub
More on Date and Time in Visual Basic 6
Previous Page | Table
of Contents | Next Page
|