How do I get the current time in a different TimeZone in 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.

1 Answer

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;
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like