Solved

date change

Posted on 2004-04-26
4
197 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

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

16 Experts available now in Live!

Get 1:1 Help Now