Solved

Set System Time

Posted on 2003-11-06
10
747 Views
Last Modified: 2012-08-14
Hi experts,

I have a question that needs your help. I need to set the system time using my software.

Therefore i used the SetSystemTime(lpSystemTime) Windows API.

However I can only set the year, month, day, minutes and seconds only.
I cannot set the hour to the time I want. For example, i set it to be 0 (which is suppose to set to 12 am) but the system time is changed to 8 am instead. There is an offset of 8 hours. when i set the time to be later, there might be a possibility that the day is changed to the following day.

How can i overcome this problem? Please advice.

Thx...

Colourblind82
0
Comment
Question by:colourblind82
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9698644
I think you can just do

i.e.
Time = "10:06:37 PM"

to set time
0
 

Author Comment

by:colourblind82
ID: 9698875
Thanks

Like

Time = "hh:mm:ss AMPM"

and

Date = "mm/dd/yyyy"

but how about the Window API? If it is to be used in C++, what should i put in the parameters?
0
 
LVL 5

Expert Comment

by:rajaamirapu
ID: 9699082
see this example
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()
    'KPD-Team 2000
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    Dim lpSystemTime As SYSTEMTIME
    lpSystemTime.wYear = 2000
    lpSystemTime.wMonth = 1
    lpSystemTime.wDayOfWeek = -1
    lpSystemTime.wDay = 24
    lpSystemTime.wHour = 23
    lpSystemTime.wMinute = 26
    lpSystemTime.wSecond = 0
    lpSystemTime.wMilliseconds = 0
    'set the new time
    SetSystemTime lpSystemTime
0
 

Author Comment

by:colourblind82
ID: 9705256
I tried with the source codes that you provide but the system date is changed to 25 January 2000 7:26 AM instead. The hour is different and the day is changed to the following day. Has it got to do with my OS? I am using Window XP Home and Window 2000.
0
 
LVL 26

Expert Comment

by:EDDYKT
ID: 9706803
The SetSystemTime function sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Expert Comment

by:anoop_chekkattu
ID: 9717070
please try this


dim s as string
s="time 23:00:00"
shell(s,0)                     'for changing time


s="date 12-30-2003"
shell(s,0)                      'for chenging date
0
 

Expert Comment

by:anoop_chekkattu
ID: 9717134
sorry..
0
 

Expert Comment

by:anoop_chekkattu
ID: 9717208
try this

Dim MyTime, MyDate
MyTime = #4:35:17 PM#
Time = MyTime

MyDate = #10/10/2010#
Date = MyDate
0
 

Author Comment

by:colourblind82
ID: 9719836
Ok I can change the date and time using

Time = "hh:mm:ss AMPM"

and

Date = "mm/dd/yyyy"

can anyone tell me how to use SetSystemTime API so that the time and date is what i want. Or is there a better API to use?

0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 50 total points
ID: 9721663
You have subract the UTC hour and local hour different from the struct

use this to get system time and you will know the hour differenet

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
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'KPDTeam@Allapi.net
    Dim SysTime As SYSTEMTIME
    'Set the graphical mode to persistent
    Me.AutoRedraw = True
    'Get the system time
    GetSystemTime SysTime
    'Print it to the form
    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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now