HOWTO: Set the System Time

Last reviewed: July 14, 1997
Article ID: Q154009
The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit only, for Windows, version 4.0

SUMMARY

The Win32 API offers the functionality to change the system time on the local machine. The change will take place immediately without the need for a reboot. Below is a code sample showing how to do this.

MORE INFORMATION

  1. Start a new project in Visual Basic. Form1 is created by default.

  2. 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
    
    

  3. Run the project by pressing the F5 key. Check the system time and date in the Control Panel or on the system. Do the same in Windows 95. It should have changed to reflect the settings from the code.

REFERENCES

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, you would have to create a DLL that does a generic thunk to the 32-bit API SetSystemTime.

For more information, please see article Q104009 on the Microsoft Developer Network (MSDN) and the article outline in the Microsoft Systems Journal (MSJ) for June 1994.


Keywords : kbusage PrgOther vb432 vb4win vb5all vb5howto kbhowto
Version : 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 14, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.