Solved

date change

Posted on 2004-04-26
4
199 Views
Last Modified: 2010-04-05
Hey,
I have a little problem with my program, it should change the date to (somedate) run a program, wait 5s and change it back.

Tom

--snip--

procedure TForm1.FormCreate(Sender: TObject);
var
myDate,newDate : TDateTime;
myYear, myMonth, myDay : Word;
begin
DecodeDate(myDate, myYear, myMonth, myDay);
myDate := StrToDate('04/24/2003');
WinExec('c:\bin\date', SW_SHOWNORMAL);
Sleep(5000);
newDate := EncodeDate(myYear, myMonth, myDay);
ReplaceDate(myDate, newDate);

//myDate := RecodeDate(myDate, myYear, myMonth, myDay);
Application.Terminate;

end;
0
Comment
Question by:T0masz
  • 2
4 Comments
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10920853

   You do not replace the system date ...

   use the API SetLocalTime

   example :

procedure TForm1.Button1Click(Sender: TObject);
var
  SysTime : TSystemTime;
begin
  with SysTime do
    begin
      wYear         := 2004;
      wMonth        := 4;
      wDayOfWeek    := 3;
      wDay          := 4;
      wHour         := 20;
      wMinute       := 51;
      wSecond       := 0;
      wMilliseconds := 0;
    end;
  SetLocalTime(SysTime);
end;
0
 
LVL 12

Expert Comment

by:Ivanov_G
ID: 10920893

  use can use also this function to convert TDateTime to TSystemTime and back

  function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 10920962
An example:

procedure TForm1.FormCreate(Sender: TObject);
var  myOldDate, myDate:   TDateTime;
     dwMark:              LongWord;
     sysTime:             SYSTEMTIME;
begin

  // Persist the current date
  dwMark:=GetTickCount;
  myOldDate:=Now;

  // Example date setting
  myDate:=StrToDate('04/24/2003');

  // Decode the date (use the current time)
  with sysTime do
  begin
     DecodeDate(myDate, wYear, wMonth, wDay);
     DecodeTime(myOldDate, wHour, wMinute, wSecond, wMilliseconds);
  end;

  // Set the time
  SetLocalTime(sysTime);

  // Do whatever
  // WinExec('c:\bin\date', SW_SHOWNORMAL);
  Sleep(5000);

  // Update with the number of milliseconds that we were delayed
  dwMark:=GetTickCount-dwMark;
  myOldDate:=myOldDate+(dwMark / MSecsPerDay);

  // Reset the date time
  with SysTime do
  begin
     DecodeDate(myOldDate, wYear, wMonth, wDay);
     DecodeTime(myOldDate, wHour, wMinute, wSecond, wMilliseconds);
  end;

  // Set the time
  SetLocalTime(sysTime);

  // Finished
  // Application.Terminate;

end;

The dwMark variable (seeded with GetTickCount) is used for keeping track of how many ms elapsed while the time was changed.

Hope this helps,
Russell

0
 

Author Comment

by:T0masz
ID: 10921181
Thanks to all of you, but rllibby was the closest one, plus I really dont like using TSystemTime because you have to put all the parameters date,time so on and on one station it didnt work correctly for some reason.

Tom
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

828 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