function TWDateToWestDate(MaskStr,s:string):TDate; //功能: MASKSTR:日期定義格式串,S:屏幕錄入日期字符 // 返回值為日期變量,如是1900/1/1,則為非法值 //范圍:屏幕錄入日期字符串,轉換為可寫入和查詢庫表的日期格式 var i,j,n,len:integer; arrDate:array [1..3] of string; //arrDate[1]:年 arrDate[2]:月 arrDate[3]:日 tmp1:string; DateSepra1:string; DateSepra2:array [1..2] of string; a:Char; // ConvetDate:TDateTime; begin TWDateToWestDate:=encodedate(1900,1,1); //返回值賦初始值 for i:=1 to 3 do arrDate[i]:=''; len:=Length(MaskStr); j:=1; n:=0; if Length(Trim(s))<6 then //錄入日期字符串長度小于6退出 exit; for i:=1 to len do //分析MASKSTR 賦值与 arrNum,arrNum1,LenYear,arrDate begin a:=MaskStr[i]; case a of 'Y':begin Inc(n); arrDate[1]:=arrDate[1]+s[n]; //得出年 end; 'M':begin Inc(n); arrDate[2]:=arrDate[2]+s[n]; //得出月 end; 'D':begin Inc(n); arrDate[3]:=arrDate[3]+s[n]; //得出日 end; else begin DateSepra1:=MaskStr[i]; DateSepra2[j]:=s[i]; Inc(j); if Length(MaskStr)=Length(s) then Inc(n); end; end; end; //判斷分隔符是否正确 if Length(MaskStr)=Length(s) then if DateSepra2[1]<>DateSepra2[2] then exit; if Length(MaskStr)<>Length(s)+2 then if DateSepra1<>DateSepra2[1] then exit; try len:=Length(arrDate[1]); if len=2 then begin if strToInt(arrDate[1])<50 then arrDate[1]:=IntToStr(strToInt(arrDate[1])+2000) else if (strToInt(arrDate[1])>=50)and(strToInt(arrDate[1])<=99) then arrDate[1]:=IntToStr(strToInt(arrDate[1])+1900); end else if len=3 then begin if strToInt(arrDate[1])<910 then arrDate[1]:=IntToStr(strToInt(arrDate[1])+2000) else if (strToInt(arrDate[1])>=911)and(strToInt(arrDate[1])<=999) then arrDate[1]:=IntToStr(strToInt(arrDate[1])+1000); end; TWDateToWestDate:=EnCodeDate(StrToInt(arrDate[1]),StrToInt(arrDate[2]),StrToInt(arrDate[3])); exit; except exit; end; end;
如果是日期型就不存在合法检测,只有可能是字符型可以用 try StrToDateTime('String of Date'); except On EConvertError do .... end;两个日期隔多少天: FormatFloat('0',DateTime2-DateTime1);
2、两日期值相减就是相隔天数
2、两日期值相减就是相隔天数
2、两日期值相减就是相隔天数
1)我的理解是:1995/02/12 YYYY/MM/DD就合法 1996-02-12 不合法
不知可對
1)我的理解是:1995/02/12 YYYY/MM/DD就合法 1996-02-12 不合法
不知可對
//功能: MASKSTR:日期定義格式串,S:屏幕錄入日期字符
// 返回值為日期變量,如是1900/1/1,則為非法值
//范圍:屏幕錄入日期字符串,轉換為可寫入和查詢庫表的日期格式
var
i,j,n,len:integer;
arrDate:array [1..3] of string; //arrDate[1]:年 arrDate[2]:月 arrDate[3]:日
tmp1:string;
DateSepra1:string;
DateSepra2:array [1..2] of string;
a:Char;
// ConvetDate:TDateTime;
begin
TWDateToWestDate:=encodedate(1900,1,1); //返回值賦初始值
for i:=1 to 3 do arrDate[i]:='';
len:=Length(MaskStr);
j:=1; n:=0;
if Length(Trim(s))<6 then //錄入日期字符串長度小于6退出
exit;
for i:=1 to len do //分析MASKSTR 賦值与 arrNum,arrNum1,LenYear,arrDate
begin
a:=MaskStr[i];
case a of
'Y':begin
Inc(n);
arrDate[1]:=arrDate[1]+s[n]; //得出年
end;
'M':begin
Inc(n);
arrDate[2]:=arrDate[2]+s[n]; //得出月
end;
'D':begin
Inc(n);
arrDate[3]:=arrDate[3]+s[n]; //得出日
end;
else
begin
DateSepra1:=MaskStr[i];
DateSepra2[j]:=s[i];
Inc(j);
if Length(MaskStr)=Length(s) then
Inc(n);
end;
end;
end;
//判斷分隔符是否正确
if Length(MaskStr)=Length(s) then
if DateSepra2[1]<>DateSepra2[2] then
exit;
if Length(MaskStr)<>Length(s)+2 then
if DateSepra1<>DateSepra2[1] then
exit;
try
len:=Length(arrDate[1]);
if len=2 then
begin
if strToInt(arrDate[1])<50 then
arrDate[1]:=IntToStr(strToInt(arrDate[1])+2000)
else
if (strToInt(arrDate[1])>=50)and(strToInt(arrDate[1])<=99) then
arrDate[1]:=IntToStr(strToInt(arrDate[1])+1900);
end
else
if len=3 then
begin
if strToInt(arrDate[1])<910 then
arrDate[1]:=IntToStr(strToInt(arrDate[1])+2000)
else
if (strToInt(arrDate[1])>=911)and(strToInt(arrDate[1])<=999) then
arrDate[1]:=IntToStr(strToInt(arrDate[1])+1000);
end;
TWDateToWestDate:=EnCodeDate(StrToInt(arrDate[1]),StrToInt(arrDate[2]),StrToInt(arrDate[3]));
exit;
except
exit;
end;
end;
StrToDateTime('String of Date');
except
On EConvertError do ....
end;两个日期隔多少天: FormatFloat('0',DateTime2-DateTime1);