Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# DateTime Code

Posted on 1999-10-18
Medium Priority
209 Views
Has anyone got any code that gives me the difference between two datetimes.  i.e I want to use 01/01/1990 10:00:00 compared with 02/01/1990 11:42:56 and get the result 1 day, 1 hour, 42 mins and 56 secs.
Thanks

John.
0
Question by:WoodyJ3
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2
• 2
• +1

LVL 3

Expert Comment

ID: 2135271
Subtract one from the other, and multiply by 86400.  This is the difference between two TDateTimes in seconds.
0

LVL 15

Expert Comment

ID: 2135274
WoodyJ3,

reject the given answer. The correct way of doing what you want is:

Var
D1, D2, DifD : TDateTime;
Days, HH, MM, SS   : word;
begin

{ D1 and D2 receive the values from a function or something }

DifD := D2 - D1; // considering  D2 > D1
Days := trunc(DifD);
DecodeTime(DifD, HH, MM, SS);
{ At this point Days has the number of vays between the 2 dates and HH, MM and SS have the values for hours, minutes and seconds }

end;

yours,

Alex
0

LVL 7

Expert Comment

ID: 2135280
following..
0

Author Comment

ID: 2135297
Sorry, simonet had what I wanted.
0

Author Comment

ID: 2135298
simonet, post an answer for the points.

Thanks

John.
0

LVL 3

Expert Comment

ID: 2135302
Reject or not, my answer is correct and complete.  A TDateTime is stored with the date in the whole part (in days from 30/12/1899), and the part of the day in the fractional part (for instance, .5 = 1/2 day, i.e. 12 hours).  This means that a single TDateTime is in days, and subtracting one from another gives days.  The difference of two = the difference in days, multiplying by 86400, gives the difference in seconds, and it's far quicker than calling DecodeTime.
0

Author Comment

ID: 2135310
Sorry but my question was quite clear.  I wanted the answer in seconds, Minutes, Hours, Days, Months and years not just seconds.

John.
0

LVL 15

Accepted Solution

simonet earned 200 total points
ID: 2135557
Thank you, John. Here it goes again, now as an answer:

Var
D1, D2, DifD : TDateTime;
Days, HH, MM, SS   : word;
begin
{ D1 and D2 receive the values from a function or something }
DifD := D2 - D1; // considering  D2 > D1
Days := trunc(DifD);
DecodeTime(DifD, HH, MM, SS);
{ At this point Days has the number of vays between the 2 dates and HH, MM and SS have the values for hours, minutes and seconds }

end;

sburck,

If John wanted only seconds, then I agree your answer would be correct. However, in order to transform seconds in D, H, M, S, the math envolved, even Â´though it is easy stuff, would make the routine slower than using Trunc and DecodeTime. Besides, by using Trunc and DecodeTime, the function becomes very short and easy to read, which easens the maintenance process.

yours,

Alex
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small printâ€¦
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, thâ€¦
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediafâ€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrâ€¦
###### Suggested Courses
Course of the Month8 days, 18 hours left to enroll