Link to home
Start Free TrialLog in
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?
Avatar of TName
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;
Avatar of wildzero

ASKER

Looks good, just testing now
ASKER CERTIFIED SOLUTION
Avatar of TName
TName

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
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...
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).
Ah thats sound like it :-)
:D Thanks a lot!
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
Hmmm when I do
OldLocal:= IncSecond(Now(),secDif*(-1));
I just get the date (like 8/09/2006) with no time....
ops
I was doing DateToStr NOT DateTimeToStr

:P
sorry