Solved

Datetime diference?

Posted on 2002-07-20
6
249 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
[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
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month3 days, 13 hours left to enroll

630 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