Solved

Datetime diference?

Posted on 2002-07-20
6
242 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

15 Experts available now in Live!

Get 1:1 Help Now