Solved

Datetime diference?

Posted on 2002-07-20
6
244 Views
Last Modified: 2010-04-04
I have two datetime strings in 2 edit boxes. How can I find the number of hours between these two values even if it exceeds 24 hrs.

eg: 7/21/2002 1:15:25 AM minus 7/22/2002 2:15:28 PM

thnks
0
Comment
Question by:drnadeem
6 Comments
 
LVL 7

Accepted Solution

by:
Cynna earned 50 total points
ID: 7167199
Very simple:

function HoursDifference(StartDateTime, EndDateTime: String): Integer;
var d1,d2: TDateTime;
begin
  DateSeparator:='/';  ShortDateFormat:='mm/dd/yyyy';
  TimeSeparator:=':';  LongTimeFormat:='h:m:s';
  d1:=StrToDateTime(StartDateTime);
  d2:=StrToDateTime(EndDateTime);
  Result:=Round((d2-d1)*24);
end;

//DEMO:
//---------

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('The difference between '+Edit1.Text+' and '+Edit2.Text+' is '+
              IntToStr(HoursDifference(Edit1.Text, Edit2.Text))+' hours.');
end;
0
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7167371
drdaem if you have Delphi6 then you can use the DateUtils unit and the HoursBetween function
there are also DaysBetween, SecondsBetween, ... check out the DateUtils unit :)
0
 

Expert Comment

by:Chalo
ID: 7167933
Try this:
Edit3.Text := DateTimeToStr((StrToDateTime(Edit1.Text) -              StrToDateTime(Edit2.Text))/24);
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 7

Expert Comment

by:Cynna
ID: 7168103
Chalo,

Did you tried out your proposed answer?

I see you are new to EE. Proposing an answer is generally not considered a good practice here. But if you propose it anyway, you should be sure it works, and it's a different one than other poster suggested. In this case, neither is true.
In the future, please consider posting a comment and let the asker choose the right answer for himself.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7170133
only a b-grade for a perfect work ?

:-(
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7170220
:-) ...

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now