Recent

Author Topic: I want to convert TFloatSpinEdit value to time (HH/MM/SS)  (Read 2015 times)

laznoob32

  • New member
  • *
  • Posts: 7
I want to convert TFloatSpinEdit value to time (HH/MM/SS)
« on: February 01, 2019, 09:04:14 pm »
Hi everyone,

I want to convert TFloatSpinEdit value to time. By time, I don't mean time of the day, I mean that if I enter 45.5 into the TFloatSpinEdit, the application will return 45:30:00.

This is what I have so far, but I fear that I have used the wrong function - StrToTime - which is part of TDateTime (I don't want time of the day). This is what I have so far:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   Spin;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Edit1: TEdit;
  18.     FloatSpinEdit1: TFloatSpinEdit;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.   myTime : TDateTime;
  29.   TimeString:string;
  30.   TimeDec: real;
  31.  
  32. implementation
  33.  
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37.  
  38. procedure TForm1.Button1Click(Sender: TObject);
  39. begin
  40.   TimeDec := FloatSpinEdit1.Value;
  41.   TimeString:=FloatToStr(TimeDec);
  42.   myTime := StrToTime(TimeString);
  43.   ShowMessage('myTime = '+TimeToStr(myTime));
  44. end;
  45.  
  46. end.    

This code doesn't allow for a value more than 23, and it doesn't allow for decimals either.

Does anybody know of a possible solution?
Thanks in advance,
laznoob

ASerge

  • Hero Member
  • *****
  • Posts: 2481
Re: I want to convert TFloatSpinEdit value to time (HH/MM/SS)
« Reply #1 on: February 01, 2019, 09:28:28 pm »
I want to convert TFloatSpinEdit value to time. By time, I don't mean time of the day, I mean that if I enter 45.5 into the TFloatSpinEdit, the application will return 45:30:00.
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. uses SysUtils;
  5.  
  6. var
  7.   TimeDec: Double = 45.5;
  8. begin
  9.   Writeln(FormatDateTime('[hh]:nn:ss', TimeDec / HoursPerDay, [fdoInterval]));
  10.   Readln;
  11. end.
« Last Edit: February 01, 2019, 09:30:03 pm by ASerge »

laznoob32

  • New member
  • *
  • Posts: 7
Re: I want to convert TFloatSpinEdit value to time (HH/MM/SS)
« Reply #2 on: February 01, 2019, 09:38:17 pm »
I want to convert TFloatSpinEdit value to time. By time, I don't mean time of the day, I mean that if I enter 45.5 into the TFloatSpinEdit, the application will return 45:30:00.
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$MODE OBJFPC}
  3.  
  4. uses SysUtils;
  5.  
  6. var
  7.   TimeDec: Double = 45.5;
  8. begin
  9.   Writeln(FormatDateTime('[hh]:nn:ss', TimeDec / HoursPerDay, [fdoInterval]));
  10.   Readln;
  11. end.

Fantastic, your solution works perfectly. Thank you ASerge.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: I want to convert TFloatSpinEdit value to time (HH/MM/SS)
« Reply #3 on: February 01, 2019, 09:57:47 pm »
If you want to avoid a dependence on SysUtils you can use a function like this:
Code: Pascal  [Select][+][-]
  1. function HoursDoubleToStr(anHourValue: Double): String;
  2. var
  3.   h, m, s: Word;
  4.   fraction: Double;
  5.   tmp: String;
  6. begin
  7.   h := trunc(anHourValue);
  8.   fraction := frac(anHourValue);
  9.   s := trunc(fraction * 3600);
  10.   m := s div 60;
  11.   s := s - m * 60;
  12.   WriteStr(Result, h);
  13.   WriteStr(tmp, m);
  14.   if Length(tmp) = 1 then
  15.     tmp := '0' + tmp;
  16.   Result := Result + ':' + tmp;
  17.   WriteStr(tmp, s);
  18.   if Length(tmp) = 1 then
  19.     tmp := '0' + tmp;
  20.   Result := Result + ':' + tmp;
  21. end;

 

TinyPortal © 2005-2018