Avatar of wildzero
wildzero
 asked on

Different between dates/time

Hi there,

I got two dates formatted like this
2006-09-07T16:02:20.000Z

and
2006-09-10T21:36:56.510Z

What I need to be able to do is figure out the time difference between those two, ie - 5 mins or 1 hour, 1 day etc.
Any ideas on where to start?
Delphi

Avatar of undefined
Last Comment
wildzero

8/22/2022 - Mon
TName

//For example you can try something like this:

uses {...}DateUtils;

{...}

procedure TForm1.Button1Click(Sender: TObject);

    function ReadTime(str:String):TDateTime;
    var y,m,d,h,min,s,ms:Integer;
    begin
       y:=StrToInt(Copy(str,1,4));
       m:=StrToInt(Copy(str,6,2));
       d:=StrToInt(Copy(str,9,2));
       h:=StrToInt(Copy(str,12,2));
       min:=StrToInt(Copy(str,15,2));
       s:=StrToInt(Copy(str,18,2));
       ms:=StrToInt(Copy(str,21,2));

       result:=EncodeDateTime(y, m, d, h, min, s, ms);

    end;

  var
  s1,s2:String;
  dt1,dt2:TDateTime;
  begin
  s1:='2006-09-07T16:02:20.000Z';
  s2:='2006-09-10T21:36:56.510Z';

  dt1:=ReadTime(s1);
  dt2:=ReadTime(s2);
  ShowMessage(IntToStr(SecondsBetween(dt1,dt2)));
end;
wildzero

ASKER
Looks good, just testing now
ASKER CERTIFIED SOLUTION
TName

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
wildzero

ASKER
Great :-)
Just 1 question now...

Based on the above,
s1 = old time in GMT
s2 = time now in GMT

how can I make it so I can get
a1 = Current time local zone - TimeDifference of (s2 - s1) (as above) so would be just taking off the seconds from your above calculation?

I think it would be like, do your above and get
 secDif:=SecondsBetween(dt1,dt2);

then do
OldLocalTime := DecSec(Date(now), secDif);
So, Decrease the seconds of the Datenow by the secDif from above? That make sense....

I have upped the points...
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
TName

Not sure if I understand you correctly...
so let's say we have

OldGMT, NowGMT:TDateTime;
and
OldLocal, NowLocal:TDateTime;

you know the values of OldGMT an NowGMT (and of course NowLocal) and you want to find out OldLocal?

OldLocal:= IncSecond(Now(),secDif*(-1));  //for going backwards in time, you can use IncSecond with a negative value
ShowMessage(DateTimeToStr(OldLocal));


But keep in mind that SecondsBetween only returns full seconds, if you want to work with milliseconds difference, either use MilliSecondsBetween,
or (for large time spans) SecondSpan. Secondspan will return seconds with second fractions (as a Double).
wildzero

ASKER
Ah thats sound like it :-)
:D Thanks a lot!
TName

You're welcome - for UTC to local, local to UTC time conversions (and other useful stuff), see:

http://www.thedelphimagazine.com/samples/1175/article.htm
http://17slon.com/gp/gp/gptimezone.htm
and the Jedi project's JclDateTime.pas
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
wildzero

ASKER
Hmmm when I do
OldLocal:= IncSecond(Now(),secDif*(-1));
I just get the date (like 8/09/2006) with no time....
wildzero

ASKER
ops
I was doing DateToStr NOT DateTimeToStr

:P
sorry