Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current time in a different TimeZone in Delphi?

Tags:

delphi

How do i get current time from different time zones in Delphi. If i use TidSNTP it will only give me the time zone in my locale.

like image 920
Heleno Sales Mesquita Avatar asked Jul 01 '12 09:07

Heleno Sales Mesquita


1 Answers

You can easily convert local time to different time zone with delphi-tzdb (Time Zone Database for Delphi).

Following is example from its documentation.

var
  LSydney: TTimeZone;
  LMadeUpLocalTime, LUniversalTime,
    LSydneyTime: TDateTime;
begin
  // Get the Sydney time zone 
  LSydney := TBundledTimeZone.GetTimeZone('Australia/Sydney');

  // Encode a local date/time value -- 14th March 2009 at 12:45:00 PM 
  LMadeUpLocalTime := EncodeDateTime(2009, 03, 14, 12, 45, 00, 00);

  // Find out what was the time in Sydney at that moment 
  LUniversalTime := TTimeZone.Local.ToUniversalTime(LMadeUpLocalTime);
  LSydneyTime := LSydney.ToLocalTime(LUniversalTime);

  WriteLn(Format('When in my time zone the time was %s, in Sydney it was %s.',
   [DateTimeToStr(LMadeUpLocalTime), DateTimeToStr(LSydneyTime)]));
end;
like image 76
Pol Avatar answered Oct 18 '22 01:10

Pol