Bootstrap

delphi-日期转换 加减调用

(日期转换 加减调用)

//日期转换 加减调用 如果出错这返回 1009,01,01
function MyStrToDateDef(const str:string):TDateTime;
var
  y,m,d:Word;
  defValue:TDateTime;
begin
  defValue:=EncodeDate(1009,01,01);
  if Length(str)<>6 then Result := defValue;
  y := StrToIntDef(leftStr(str,4),-1);
  m := StrToIntDef(MidStr(str,5,2),-1);
  d := StrToIntDef(RightStr(str,2),-1);
  if (y<1) or (m<1) or (m>12) or (d<1) or (d>31) then Result := defValue
  else Result := EncodeDate(y,m,d);
end;

//调用 
var  
  gpDate:TDateTime;
begin 
//  defDate := EncodeDate(1970,1,1);  
      gpDate := MyStrToDateDef(20100505);
      if now-gpDate<=3 then       //与当前时间加减
      begin
       showmessage('y')
      end else showmessage('n');
end ;

;