--> Set System Date on Delphi | Delphi Tips Trick

For Newbie

Tuesday, 15 November 2011

Set System Date on Delphi

| Tuesday, 15 November 2011
Tricks on how to change the date in the system.

Function declaration:

Method # 1:
NewDate function (const Day, Month, Year: Word): Boolean;
var
   ST: TSystemTime;
begin
   GetSystemTime (ST);
   with ST do
   begin
     wYear: = Year;
     wMonth: = Month;
     wday: = Day - 1;
   end;
   Result: = SetSystemTime (ST);
end;

Method # 2:
NewDateByDate function (const D: TDate): Boolean;
var
   Day, Month, Year: Word;
begin
   DecodeDate (D, Year, Month, Day);
   Result: = NewDate (Day, Month, Year);
end;

Examples of implementation # 1 (by using the first function):
procedure TForm1.Button1Click (Sender: TObject);
begin
   / / Date in the system will be changed to August 17, 1945
   if NewDate (17, 8, 1945) then ShowMessage ('Success!')
   else
     ShowMessage ('Failed!');
end;

Example implementations # 2 (by using a second function):
procedure TForm1.Button1Click (Sender: TObject);
begin
   / / Date in the system will be converted to 2 days before
   NewDateByDate if (Now - 2) then ShowMessage ('Success!')
   else
     ShowMessage ('Failed!');
   {By using TDateTimePicker
    NewDateByDate (DateTimePicker1.Date);}
end;

Related Posts

No comments: