Str2Time Function
Converts a string containing a Time into a DateTime.

Unit
QESBPCSDateTime

Declaration
Function Str2Time(const TimeStr: string): TDateTime;

Description
The following are all exceptable separators for entry: [':', '.'] though the current TimeSeparator will be used for display Times can be entered without Separators but Leading Zeroes must then be used and the format is assumed to be either HHMM or HHMMSS.

Parameters
TimeStr The String to convert.

Category
Date/Time Conversion Routines

Implementation

function Str2Time (const TimeStr: string): TDateTime;
var
     S: string;
begin
     S := Trim (TimeStr);
     if S = '' then
     begin
          Result := 0.0;
          Exit;
     end;

     if IsDigitStr (S) then
     begin
          case Length (S) of
               4: S := LeftStr (S, 2) + TimeSeparator + RightStr (S, 2);
               6: S := LeftStr (S, 2) + TimeSeparator + Copy (S, 3, 2)
                    + TimeSeparator + Copy (S, 5, 2);
          end;
     end;

     try
          // Allow '.' and ':' as valid alternatives for TimeSeparator
          S := ReplaceChStr (S, '.', TimeSeparator);
          S := ReplaceChStr (S, ':', TimeSeparator);
          // S := ReplaceChStr ( S, ' ', TimeSeparator);

          // Remove trailing Separator if any
          if S [Length (S)] = TimeSeparator then
          begin
               S := LeftStr (S, Length (S) - 1);
               if S = '' then
               begin
                    Result := 0.0;
                    Exit;
               end;
          end;

          //Frac ensures the Date Component is 0
          Result := Frac (StrToTime (S));
     except
          Result := 0.0;
          if ESBRaiseDateError then
               raise;
     end;
End;


HTML generated by Time2HELP
http://www.time2help.com