How To Set the System Time
This article was previously published under Q154009
This article has been archived. It is offered "as is" and will no longer be updated.
The Win32 SetSystemTime API function offers the functionality to change thesystem time on the local machine. The change will take place immediatelywithout the need for a reboot. This article illustrates how to create asample project that sets the system time using the SetSystemTime function. The SetSystemTime function sets the system time based on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England.
- Start a new project in Visual Basic. Form1 is created by default.
- Add the following code to the General Declarations section of Form1:
Option Explicit 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 Private Declare Function SetSystemTime Lib "kernel32" (lpSystemTime _ As SYSTEMTIME) As Long Private Sub Form_Load() Dim lReturn As Long Dim lpSystemTime As SYSTEMTIME lpSystemTime.wYear = 1996 lpSystemTime.wMonth = 6 lpSystemTime.wDayOfWeek = 5 lpSystemTime.wDay = 28 lpSystemTime.wHour = 9 lpSystemTime.wMinute = 42 lpSystemTime.wSecond = 0 lpSystemTime.wMilliseconds = 0 lReturn = SetSystemTime(lpSystemTime) End Sub
- Run the project by pressing the F5 key. Check the system time and date in the Control Panel. The time should have changed to reflect the UTC-based time settings used in the sample.
The SYSTEMTIME Type structure is as follows:
WYear Integer-The current year. WMonth Integer-The current month. January is 1. WDayOfWeek Integer-The current day of the week. Sunday is 0. WDay Integer-The current day of the month. WHour Integer-The current hour. wMinute Integer-The current minute. wSecond Integer-The current second. wMilliseconds Integer-The current millisecond.
To change the system time on a Win32 platform from 16-bit Visual Basic, youwould have to create a DLL that does a generic thunk to the 32-bit APISetSystemTime.
Article ID: 154009 - Last Review: 12/04/2015 15:06:45 - Revision: 3.1
Microsoft Visual Basic 5.0 Learning Edition, Microsoft Visual Basic 6.0 Learning Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 6.0 Enterprise Edition, Microsoft Visual Basic 4.0 Standard Edition, Microsoft Visual Basic 4.0 Professional Edition, Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
- kbnosurvey kbarchive kbhowto kbapi KB154009