Link to home
Start Free TrialLog in
Avatar of DeeOmen
DeeOmenFlag for United Kingdom of Great Britain and Northern Ireland

asked on

HOURS MINS BETWEEN 2 DATES AND TIMES

hi im using a system called paris, which has Delphi as its language.

Im a novice with Delphi

I need to get the hours and mins between 2 dates and times

date 1      date2
time 1     time 2

no idea where to start any help would be great
Avatar of Geert G
Geert G
Flag of Belgium image

check the unit DateUtils
http://docwiki.embarcadero.com/Libraries/XE3/en/System.DateUtils

as you are new to Delphi

there is "implementation" and "interface" section
add the DateUtils unit to the implementation section to the uses clause

then you can use the functions/procedures of that unit

MinutesBetween or HoursBetween ... might be what you are looking for
I didn't know the city of Paris was running on Delphi
we learn something new every day ... :)
Avatar of DeeOmen

ASKER

thanks geert  yeah strange name for a system.

I was hoping maybe some help with how the code should look, been stuck with this for a few days
most of us don't have the newest "mind reading" headsets yet
so for us to be able to help, you'll have to post the code you have trouble with
Avatar of DeeOmen

ASKER

sorry about that
var
time1, time2: TTime;
date1, date2: TDate;


if (not sspDEETESTCAL.AssForm.Pac.Security.IsReadOnlyUser) and (not sspDEETESTCAL.ReadOnly) then
begin
date1 := StrToDate(edtDEETESTCAL_datea.Text);
date2 := StrToDate(edtDEETESTCAL_dateb.Text);

 lblDEETESTCAL_daysbetwen.caption:=('Day span: '+ FloatToStr(DaySpan(date1, date2)));

time1 := StrToTime(edtDEETESTCAL_timea.Text);
time2 := StrToTime(edtDEETESTCAL_timeb.Text);

lblDEETESTCAL_HoursBetween.caption:=('Hour span: '+ FloatToStr(HourSpan(time1, time2)));


end;

get an error message for the time, I have attached error
error-message-1.PNG
i expect this Paris system is not something which runs in a worlwide environment ?
there are a lot of assumptions in that code

do you have restricitions on the edit boxes ?
like what format the date/time has to be in ?

there are no checks for the date/time editboxes
not even if the edits are empty or not

i expect someone has entered an invalid format
> you'll have to check what the pc date/time format is in the country settings
my setting for time is hh:nn:ss

and you have a comma in your error message ...

you need to validate the entries before converting to date / time
as you indicated, you inherited this code ...
just a heads up

delphi is not C# or C++
you don't need to put round brackets around everything

makes it less readable

these bold brackets are useless
 lblDEETESTCAL_daysbetwen.caption:=('Day span: '+ FloatToStr(DaySpan(date1, date2)));

so are these:
if (not sspDEETESTCAL.AssForm.Pac.Security.IsReadOnlyUser) and (not sspDEETESTCAL.ReadOnly) then
Avatar of DeeOmen

ASKER

hi

this is just a test to get calculations working, no paris isn't worldwide.

the time format used in paris 00.00
for the test I am entering data into the field, once calculations are complete then i'l build in validation.
are you sure about that format
it's usually 00:00
the StrToTime uses the TimeFormat of your windows settings

unless you have explicitely changed it with the FormatSettings global functions/object
Avatar of DeeOmen

ASKER

I know geert, I agree 100% with you.  but in paris it is 00.00 which is a pain. no haven't changed any setting

I appreciate you patience
ASKER CERTIFIED SOLUTION
Avatar of Geert G
Geert G
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DeeOmen

ASKER

im sure its 00.00
you could make it even more advanced
replace all non-number characters with : and use a specific formatsetting with : to do the conversion
this function will always work.
no matter what letter, symbol or whatever you use for a time separator:

function EditStrToTime(aEditStr: string): TDateTime;
var s: string;
  I: Integer;
  fs: TFormatSettings;
begin
  s := aEditStr;
  for I := 1 to Length(aEditStr) do
    if not (s[I] in ['0'..'9']) then
      s[I] := ':';
  fs := TFormatSettings.Create(LOCALE_USER_DEFAULT);
  fs.TimeSeparator := ':';
  Result := StrToTime(S, fs);
end;

Open in new window


Tested with an 'x' as separator :

procedure TuMain.btnTestClick(Sender: TObject);
begin
 ShowMessage(FormatDateTime('dd/mm/yyyy hh:nn:ss', EditStrToTime('08x00')));
end;

Open in new window