Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Formatting Times And Dates

When working with times and dates in Visual Basic, it’s valuable knowing how to display them as strings. For example, you can use the Visual Basic Time$ and Date$ functions to get the time and date in string form, suitable for display:

Text1.Text = Time$

You can also use string comparisons here; for example, to check if the current time is past a time specified in string form, you can use code like this:

If (Time$ > AlarmSetting.Text) Then
...
End If

Besides Time$ and Date$ , you can use Now . This function refers to the current time in a numeric way, and you can use comparisons this way:

If (Now > AlarmTime) Then
...
End If

To display the current date and time using Now , you use the Format$ function. For example, this use of Format$ and Now :

Format$(Now, "dddd, mmmm d, yyy")

returns the string with the day of the week, the month, date, and year like this: “Friday, January 1, 2000”. The different format strings and what they do appear in Table 13.1—and some examples appear in Table 13.2 to make all this clearer.

Table 13.1 Date and time format strings.
String Description
d The one- or two-digit day.
dd The two-digit day. Single-digit day values are preceded by a zero.
ddd The three-character day-of-week abbreviation.
dddd The full day-of-week name.
h The one- or two-digit hour in 12-hour format.
hh The two-digit hour in 12-hour format. Single-digit values are preceded by a zero.
H The one- or two-digit hour in 24-hour format.
HH The two-digit hour in 24-hour format. Single-digit values are preceded by a zero.
m The one- or two-digit minute.
mm The two-digit minute. Single-digit values are preceded by a zero.
M The one- or two-digit month number.
MM The two-digit month number. Single-digit values are preceded by a zero.
MMM The three-character month abbreviation.
MMMM The full month name.
s The one- or two-digit seconds.
ss The two-digit seconds. Single-digit values are proceeded by a zero.
AM/PM The two-letter AM/PM abbreviation (that is, AM is displayed as “AM”).
y The one-digit year (that is, 1999 would be displayed as “9”).
yy The last two digits of the year (that is, 1999 would be displayed as “99”).
yyyy The full year (that is, 1999 would be displayed as “1999”).

Table 13.2 Formatted date and time examples.
Format Expression Result
Format$(Now, “m - d - yy”) “1-1-00”
Format$(Now, “m / d / yy”) “1 / 1 / 00”
Format$(Now, “mm - dd - yy”) “01 /01 / 00”
Format$(Now, “dddd, mmmm d, yyyy”) “Friday, January 1, 2000”
Format$(Now, “d mmm, yyyy”) “1 Jan, 2000”
Format$(Now, “hh:mm:ss mm/dd/yy”) “01:00:00 01/01/00”
Format$(Now, “hh:mm:ss AM/PM mm - dd - yy”) “01:00:00 AM 01-01-00”

Creating A Clock Program

Creating a clock in Visual Basic is easy with the timer control. To see how that works, just create a new project now and add a timer control, Timer1. Set the timer’s Interval property to 1000 (that is, a thousand milliseconds, or one second).

Next, add a label that covers most of the form and give it a large font, like 48-point Courier New. We’ll display the time in that label each time the timer ticks, so add the Timer1_Tick() event handler now:

Sub Timer1_Timer()

End Sub

All we have to do when there’s a Timer event is to update the clock, and we use the Visual Basic Time$ function to do that:

Sub Timer1_Timer()
    Display.Caption = Time$
End Sub

That’s all we need. Now the clock is functional, as shown in Figure 13.4.


Figure 13.4  A clock created with the timer control.

The code for this example is located in the clock folder on this book’s accompanying CD-ROM. If you want to create more than a simple clock—an alarm clock, for example—see the following topics in this chapter.

Creating A Stopwatch

The Testing Department is calling. Users are concerned about the time your SuperDuperDataCrunch program takes in execution—can you add a stopwatch to the program to convince them it’s really pretty fast? You think, a stopwatch?

Building a stopwatch is valuable to see how to work with elapsed time instead of simply system time. To build a stopwatch program, create a new Visual Basic project now and add two buttons, labeled “Start” and “Stop”, as well as a label control named Display (set the font in the label to something large, like 48-point Courier New). Also add a timer control, Timer1, and set its Enabled property to False so it doesn’t do anything until the user clicks the Start button.

Now when the user clicks the Start button, Command1 , we can store the current time using Now in a form-wide variable named StartTime (add StartTime to the (General) section of the form), and we can start the timer by setting its Enabled property to True:

Private Sub Command1_Click()
    StartTime = Now
    Timer1.Enabled = True
End Sub

When the user clicks the Stop button, Command2, we can stop the timer:

Private Sub Command2_Click()
    Timer1.Enabled = False
End Sub

Finally, in the Timer event, we just display the time that has elapsed from the starting time, and that time is just the difference between the current value of Now and the StartTime variable:

Sub Timer1_Timer()
    Display.Caption = Format$(Now - StartTime, "hh:mm:ss")
End Sub


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.