Solved

Set System Time

Posted on 2003-11-06
10
742 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
Comment Utility
I think you can just do

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

to set time
0
 

Author Comment

by:colourblind82
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
The SetSystemTime function sets the current system time and date. The system time is expressed in Coordinated Universal Time (UTC).
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:anoop_chekkattu
Comment Utility
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
Comment Utility
sorry..
0
 

Expert Comment

by:anoop_chekkattu
Comment Utility
try this

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

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

Author Comment

by:colourblind82
Comment Utility
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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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

6 Experts available now in Live!

Get 1:1 Help Now