Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

How to change system Date/Time settings ?
[ All Languages » VB »  Windows]

Total Hit ( 3753)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


This example will show you how to Get/Set current system time.

For Quick demo Copy/Paste following code into form1 general declaration

Click here to copy the following block
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 Sub GetLocalTime Lib "kernel32" _
    (lpSystemTime As SYSTEMTIME)

Private Declare Sub GetSystemTime Lib "kernel32" _
    (ByRef lpSystemTime As SYSTEMTIME)

Private Declare Function SetSystemTime Lib "kernel32" _
    (ByRef lpSystemTime As SYSTEMTIME) As Long

Private Sub Form_Load()
  Dim dTimeUTC As Date
  Dim dTimeLocal As Date

  Dim uTime As SYSTEMTIME
  Dim lResult As Long

  ' first get the current UTC (GMT) system time
  GetSystemTime uTime
  With uTime
    dTimeUTC = DateSerial(.wYear, .wMonth, .wDay) + _
        TimeSerial(.wHour, .wMinute, .wSecond)
  End With

  ' get the current system local time
  GetLocalTime uTime
  With uTime
    dTimeLocal = DateSerial(.wYear, .wMonth, .wDay) + _
        TimeSerial(.wHour, .wMinute, .wSecond)
  End With

  s = "Current UTC time : " & Format$(dTimeUTC, "yyyy-mm-dd hh:nn:ss") & vbCrLf
  s = s & "Current Local time : " & Format$(dTimeLocal, "yyyy-mm-dd hh:nn:ss")

  MsgBox s

  ' now add 1 hour and set the time
  dTimeUTC = DateAdd("h", 1, dTimeUTC)
  With uTime
    .wYear = Year(dTimeUTC)
    .wMonth = Month(dTimeUTC)
    .wDay = Day(dTimeUTC)
    .wHour = Hour(dTimeUTC)
    .wMinute = Minute(dTimeUTC)
    .wSecond = Day(dTimeUTC)
  End With

  lResult = SetSystemTime(uTime)

  If lResult <> 0 Then
    MsgBox "Time advanced 1 hour:" & Format$(Now, "yyyy-mm-dd hh:nn:ss"), vbOKOnly, "Advanced"
    ' and reset using VB
    dTime = DateAdd("h", -1, Now)
    Time = dTime
    Date = dTime
    MsgBox "Time reset:" & Format$(Now, "yyyy-mm-dd hh:nn:ss"), vbOKOnly, "Reset"
  Else
    MsgBox "Set system time returned error " & _
        CStr(lResult) & ":" & CStr(Err.LastDllError), vbOKOnly, "Set Failed"
  End If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.