DeeOmen
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
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
I didn't know the city of Paris was running on Delphi
we learn something new every day ... :)
we learn something new every day ... :)
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
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
so for us to be able to help, you'll have to post the code you have trouble with
ASKER
sorry about that
var
time1, time2: TTime;
date1, date2: TDate;
if (not sspDEETESTCAL.AssForm.Pac. Security.I sReadOnlyU ser) and (not sspDEETESTCAL.ReadOnly) then
begin
date1 := StrToDate(edtDEETESTCAL_da tea.Text);
date2 := StrToDate(edtDEETESTCAL_da teb.Text);
lblDEETESTCAL_daysbetwen.c aption:=(' Day span: '+ FloatToStr(DaySpan(date1, date2)));
time1 := StrToTime(edtDEETESTCAL_ti mea.Text);
time2 := StrToTime(edtDEETESTCAL_ti meb.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
var
time1, time2: TTime;
date1, date2: TDate;
if (not sspDEETESTCAL.AssForm.Pac.
begin
date1 := StrToDate(edtDEETESTCAL_da
date2 := StrToDate(edtDEETESTCAL_da
lblDEETESTCAL_daysbetwen.c
time1 := StrToTime(edtDEETESTCAL_ti
time2 := StrToTime(edtDEETESTCAL_ti
lblDEETESTCAL_HoursBetween
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
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.c aption:=('Day span: '+ FloatToStr(DaySpan(date1, date2)));
so are these:
if (not sspDEETESTCAL.AssForm.Pac. Security.I sReadOnlyU ser) and (not sspDEETESTCAL.ReadOnly) then
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.c
so are these:
if (not sspDEETESTCAL.AssForm.Pac.
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.
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
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
unless you have explicitely changed it with the FormatSettings global functions/object
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
I appreciate you patience
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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:
Tested with an 'x' as separator :
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;
Tested with an 'x' as separator :
procedure TuMain.btnTestClick(Sender: TObject);
begin
ShowMessage(FormatDateTime('dd/mm/yyyy hh:nn:ss', EditStrToTime('08x00')));
end;
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