We help IT Professionals succeed at work.

System Time

d32coder
d32coder asked
on
I'm writing a network app and I need to sychronize the system times on all the nodes with the server.

All I really need is the source code to set the system Date/Time locally.

Don
Comment
Watch Question

Try this function.


procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption:=GetsystemTimeUtc()
end;


function GetSystemTimeUTC : String;
var
   stSystemTime : TSystemTime;
begin
   Windows.GetSystemTime( stSystemTime );
   Result := DateTimeToStr( SystemTimeToDateTime( stSystemTime ) );
end;

Author

Commented:
Not quite.  I want to SET the time.

Don
procedure TForm1.Button1Click(Sender: TObject);
begin
label1.caption:=GetsystemTimeUtc();
label2.caption:=GetLocalt();
end;



I add the Local Time function:

function GetSystemTimeUTC : String;
var
   stSystemTime : TSystemTime;
begin
   Windows.GetSystemTime( stSystemTime );
   Result := DateTimeToStr( SystemTimeToDateTime( stSystemTime ) );
end;

 
function GetLocalT: String;
var
   stSystemTime : TSystemTime;
begin
   Windows.GetLocalTime( stSystemTime );
   Result := DateTimeToStr( SystemTimeToDateTime( stSystemTime ) );
end;



 
procedure TForm1.Button1Click(Sender: TObject);
 
  procedure Systemdatechange(MyDate: TDateTime);
  var
    FecSys: TSystemTime;
    nA, nM, nD: Word;
  begin
    DecodeDate(Mydate, nA,nM,nD);
    GetLocalTime(FecSys);
    FecSys.wYear  := nA;
    FecSys.wMonth := nM;
    FecSys.wDay   := nD;
    SetLocalTime(FecSys);
  end;
begin
  Systemdatechange( StrToDate('23/08/2002') );
end;

 
procedure TForm1.Button1Click(Sender: TObject);
var
   HourSistema : TSystemTime;
   NewTime:string;
begin
   {New Time}
   
   NewTime:='13:58:00';
   DateTimeToSystemTime( Date+StrToTime(NewTime),HourSistema);
   SetLocalTime(HourSistema );
end;
 

Author

Commented:
Thanks!

Don

Explore More ContentExplore courses, solutions, and other research materials related to this topic.