Solved

Set System Time

Posted on 2003-11-06
10
752 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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
 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

724 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