Link to home
Start Free TrialLog in
Avatar of ChLa
ChLaFlag for United States of America

asked on

DateTimeToStr( is giving me the wrong time. What is happening ?

Hi, I am embellishing a Multi Device program I wrote in Delphi 11. This is an FMX program. I  am using this to get the local time:

TimeStr := DateTimeToStr(TTimeZone.Local.ToLocalTime(Now)); The date and time are correct, except that the time shown is 5 hours early. I checked the time/date settings on this computer, a Windows 10 Dell laptop, Everything is correct. Correct Date, Time, Time Zone. The computer displays the correct local time and date. DateTimeTo Str gives the correct date, minutes and seconds. But it shows a time 5 hours early.

However, this: TimeStr := DateTimeToStr(TTimeZone.Local.ToUniversalTime(Now));

gives me the correct GMT(UCT, Zulu) time (Eastern Standard time + 5 hours).

Avatar of Dr. Klahn
Dr. Klahn

It would seem that it's a bit more complicated than the obvious solution.

http://delphitipsandtricks.blogspot.com/2013/05/convering-utc-to-local-time-and-vice.html
Avatar of ChLa

ASKER

So you are suggesting I take UTC time and comvert it to local time ?  Is this a known bug ?
Maybe the time is stored as local so you don't need to convert
ASKER CERTIFIED SOLUTION
Avatar of Sinisa Vuk
Sinisa Vuk
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ChLa

ASKER

Thank you. That works for me. I threw a tiny program together to test and compare the methods, partly to be sure my program wasn't causing the problem. This program demonstrates how to do get and display datetime, and the different methods. Here is a picture of the program running:

User generated image
Here is the program:
unit Unit1;     //simple multi device program to test methods for obtaining time

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.Edit, DateUtils, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  UTCNow: TDateTime;

implementation

{$R *.fmx}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Edit1.Text := DateTimeToStr(TTimeZone.Local.ToLocalTime(Now));
  Edit2.Text := DateTimeToStr(TTimeZone.Local.ToUniversalTime(Now));
  UTCNow := TTimeZone.Local.ToUniversalTime(Now);
  Edit3.Text := DateTimeToStr(UTCNow);
  Edit4.Text := DateTimeToStr(Now);
  Edit5.Text := DateTimeToStr(TTimeZone.Local.ToLocalTime(UTCNow));
end;

end.