How inc ddd hh:mm:ss into my datetime?

Hello guys

I have a maskedit where I inform ddd hh:mm:ss and I want to inc my date, I tried this code and it works fine for days but not
for hours :(

For example:  001  02:35:40 add to my date 07/11/2017 13:00:00

procedure TFDatasPrevisao.MaskEdit1Exit(Sender: TObject);
var
  dias, horas, minutos, segundos : String;
  Dt   : TDateTime;
begin
  Dt       := MaskEdit1.DateTime;
  Dias     := Copy(MaskEdit3.text, 1,3);
  Horas    := Copy(MaskEdit3.text, 5, 2);
  Minutos  := Copy(MaskEdit3.text, 8, 2);
  segundos := Copy(MaskEdit3.text, 11, 2);

  if StrToInt( dias ) > 0 then
  Begin
    dt := IncDay(dt, StrToInt( dias ))
  End;

  if StrToInt( horas ) > 0 then begin
     dt := IncHour(dt, StrToInt( horas ))
  end;

  if StrToInt( minutos ) > 0 then begin
     dt := IncMinute(dt, StrToInt( minutos ));
  end;

  MaskEdit2.Date :=  dt;

end;

Open in new window


Whats is wrong? any clue for a better way?

thanks
Alexandre
LVL 1
hidrauAsked:
Who is Participating?
 
Sinisa VukConnect With a Mentor Commented:
Do you have correct value in dt after line: Dt := MaskEdit1.DateTime ?
Check it first:
Dt       := MaskEdit1.DateTime;
ShowMessage(DateTimeToStr(dt));
...

Open in new window


I would convert all inc values to seconds first - then do Inc afterwards.
...
var
...
  ss: Integer;
begin
...
  Dias     := Copy(MaskEdit3.text, 1,3);
  Horas    := Copy(MaskEdit3.text, 5, 2);
  Minutos  := Copy(MaskEdit3.text, 8, 2);
  segundos := Copy(MaskEdit3.text, 11, 2);

  ss := 0;
  ss := ss + StrToIntDef(Dias,0) * 60 * 60 * 24; //add days
  ss := ss + StrToIntDef(Horas,0) * 60 * 60; //add hours
  ss := ss + StrToIntDef(Minutos,0) * 60; //add minutes
  ss := ss + StrToIntDef(segundos,0); //add seconds

  dt := IncSecond(dt, ss);
...

Open in new window

1
 
aikimarkConnect With a Mentor Commented:
Split the data into the day and the time values.  Once you convert the time string into a real time value, you should be able to add it to the real time value (13:00:00) of your date.  If the resulting time is less than the 13:00:00 value, then add 1 to the date increment value.  Next, increment the date value by the adjusted, if necessary, number of days
1
 
hidrauAuthor Commented:
Thanks a lot for the example and explanation
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.