Mega Code Archive

 
Categories / Delphi / Algorithm Math
 

See as returning the difference of days and hours among two dates

Title: See as returning the difference of days and hours among two dates. Question: how to make calculations the difference of days and hours among two dates? Answer: procedure TForm1.Button1Click(Sender: TObject); begin Calc_Dia_Hora(Dia, Hora, StrToDateTime(MaskEdit1.Text), StrToDateTime(MaskEdit2.TExt)); Label1.Caption := Total of Days...: + Dia;//day Label2.Caption := Total of hours..: + Hora;//hour end; procedure Calc_Dia_Hora(var ResDia, ResHora: String; DiaHoraIn, DiaHoraFin: TDateTime); var DHI, DHF: String; DiaI, DiaF, HoraI, HoraF: String; begin DHI := DateTimeToStr(DiaHoraIn); DHF := DateTimeToStr(DiaHoraFin); DiaI := Copy(DHI, 1,8); DiaF := Copy(DHF, 1,8); HoraI := Copy(DHI, 10,5); HoraF := Copy(DHF, 10,5); if (DiaHoraIn begin if (StrToTime(HoraI) begin ResDia := FloatToStr(StrToDateTime(DiaF) - StrToDateTime(DiaI)); ResHora := TimeToStr(StrToTime(HoraF) - StrToTime(HoraI)); end else begin ResDia := FloatToStr(StrToDateTime(DiaF) - StrToDateTime(DiaI) -1); ResHora := TimeToStr(StrToTime(HoraF) - StrToTime(HoraI)+ 24); end; end else ShowMessage(The final Date cannot be smaller than dates initial...!); end;