[Delphi] Delphi Tips

Delphi Tips

函数篇


函数篇

  • StrToDate()

function StrToDate(const S: string): TDateTime;
function StrToDate(const S: string;
const FormatSettings: TFormatSettings): TDateTime;

StrToDate将给定的字符串转换为日期值。 分隔符只能是为'' / '', eg: '2019-10-01', 年值假定在本世纪为0到99之间。 给定字符串只能包含有效日期。

如分隔符不是'' / '', 如''2010-12-1'', 此时抛错: ''2010-12-1'' is not a valid date'.为解决此函数固定分隔符的问题。可构造 getformat函数。

function getformat(str: string):string;
var
  I, j: Integer;
begin
  for I := 1 to length(str) do begin
    if not TryStrToInt(str[i], j)  then begin
      result := str[i];
      break;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  dt:  TdateTime;
  formatter, s: string;
begin
  s := '2010-12-1';
  formatter := getformat(s);
  showmessage(datetimetostr(strtodate(stringreplace(s, formatter, '/', [rfReplaceAll]))));
end;

语法篇

未完待续

猜你喜欢

转载自www.cnblogs.com/xianeri/p/11674042.html