Solved

Datetime diference?

Posted on 2002-07-20
6
248 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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