Avatar of ChLa
ChLa
Flag 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).

DelphiWindows 10

Avatar of undefined
Last Comment
ChLa

8/22/2022 - Mon
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
ChLa

ASKER
So you are suggesting I take UTC time and comvert it to local time ?  Is this a known bug ?
Mike McCracken

Maybe the time is stored as local so you don't need to convert
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER CERTIFIED SOLUTION
Sinisa Vuk

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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:


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.