我们要用到 VarToDateTime这个函数,以避免不同操作系统时间格式带来的格式化错误
先定义获取年龄和年龄单位的函数
function GetAgeInfo(dBirth: TDateTime; var iCnt: Integer;
dNow: TDateTime): string;
var
nY, nM, nD, nY1, nM1: Word;
begin
Result:= '岁';
iCnt := 0;
if dBirth=0 then Exit;
dBirth := Trunc(dBirth);
iCnt := YearsBetween(dNow, dBirth);
if iCnt>=2 then
begin
iCnt := Abs(YearOf(dNow)-YearOf(dBirth));
if IncYear(dBirth, iCnt)>dNow then Dec(iCnt); //第二年不到生日减一岁
Exit;
end;
Result := '天';
iCnt := DaysBetween(dNow, dBirth);
if iCnt<=60 then Exit;
Result := '月';
DecodeDate(dNow, nY, nM, nD);
DecodeDate(dBirth, nY1, nM1, nD);
iCnt := Abs((nY-nY1)*12+nM-nM1);
end;
定义获取身份证信息函数
function GetCard_Info(sCardNo:string;dNow :TDateTime;
var sBirth,sArea,SSex,sAge,sAgeUnit:string):string;
var
iLen,iSex,iAge:Integer;
dBirth:TDateTime;
begin
Result:='输入身份证号,格式不正确:'+sCardNo;
iLen:=Length(sCardNo);
if (iLen<>15) and (iLen<>18) then Exit;
try
if iLen=15 then
sBirth:='19'+Copy(sCardNo,7,2)+'-'+Copy(sCardNo,9,2)+'-'+Copy(sCardNo,11,2)
else sBirth:=Copy(sCardNo,7,4)+'-'+Copy(sCardNo,11,2)+'-'+Copy(sCardNo,13,2);
dBirth:=VarToDateTime(sBirth);
sArea:=Copy(sCardNo,1,6);
if iLen=15 then iSex:=StrToIntDef(Copy(sCardNo,15,1),1)
else iSex:=StrToIntDef(Copy(sCardNo,17,1),1);
if (iSex mod 2=0) then SSex:='女'
else SSex:='男';
sAgeUnit:=GetAgeInfo(dBirth,iAge,dNow);
sAge:=IntToStr(iAge);
Result:='';
except
on E:Exception do
begin
Result:='获取身份证信息失败'+e.Message;
end;
end;
end;
网友评论