Displaying the Date and time using the API
Windows API example in Visual Basic number 2
Displaying the Date and time using the API
This simple API example we are going to get the date and time
Start up Visual Basic as usual and enter the code below in the general declarations section of the form.
Option Explicit
Private Declare Sub GetSystemTime Lib “kernel32″ (lpSystemTime As SYSTEMTIME)
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Now enter the following in the Form_Load event procedure
Private Sub Form_Load()
Dim SysTime As SYSTEMTIME
Form1.AutoRedraw = True
GetSystemTime SysTime
Me.Print “The System Date is:” & SysTime.wMonth & “-” & SysTime.wDay & “-” & SysTime.wYear
Me.Print “The System Time is:” & SysTime.wHour & “:” & SysTime.wMinute & “:” & SysTime.wSecond
End Sub
Now run the program (Press F5) and all going well you should see the date and time of your system. Here is what was displayed
on the test system.

Analysis
The first thing is a user defined type SYSTEMTIME, this contains eight members all of which are integers. As you can see from the code it is
fairly easy to see due tothe naming used what these members are used for
Next we insert the GetSystemTime API function which is used to retrieve the memory information.
Now in the Form_Load event we declare various code, the first snippet declares a variable SysTime of type SYSTEMTIME. The SysTime
variable is passed to the GetSystemTime API function which fills in the information in to the SYSTEMTIME user defined type.
We then display the date and time on the form.
Further Projects
Using a timer build a digital clock.
Put the time in atext box,on the Form’s caption and on the status bar.
























