var year,month,days:integer;read(year,month);
case month of
1,3,5,7,8,10,12:days:=31;
4,6,9,11:days:=30;
2: if (year mod 400=0) or( (year mod 4=0) and (year mod 100<>))
then days:=29 else days:=28;
end;
case month of
1,3,5,7,8,10,12:days:=31;
4,6,9,11:days:=30;
2: if (year mod 400=0) or( (year mod 4=0) and (year mod 100<>))
then days:=29 else days:=28;
end;
2: if (year mod 400=0) or( (year mod 4=0) and (year mod 100<> 0 ))
----
1,3,5,7,8,10,12:days=31;
2: if(year mod 400=0 and not (year mod 100=0) and (year mod 4=0)
then days:29;
else
days:28;
4,6,9,10:days:=30;
else showmessage('输入的月份是无效的!");
end;
Yr, Mnth, Day: Word;
begin
DecodeDate(Date, Yr, Mnth, Day);
cae mnth of
1,3,5,7,8,10,12:day=31;
2: if IsLeapYear(Yr) then day:=29;
else day:=28;
4,6,9,11:day:=30;
end;
end;
俺设定的日期为string型,格式为'yyyy-mm-dd'
这段代码就是判断用户输入的日期是否是合法日期
function CheckDateString(DateString:string):bool;
var year,month,day,maxday:integer;
begin
result:=true;
//长度检查
if length(DateString)<>10 then
begin
result:=false;
exit;
end;
//格式合法性
if (DateString[5]<>'-') or (DateString[8]<>'-') then
begin
result:=false;
exit;
end;
//年合法性
try
year:=strtoint(copy(DateString,1,4));
except
result:=false;
exit;
end;
if year<1 then
begin
result:=false;
exit;
end;
//月合法性
try
month:=strtoint(copy(DateString,6,2));
except
result:=false;
exit;
end;
if (month<1) or (month>12)then
begin
result:=false;
exit;
end;
//日合法性
try
day:=strtoint(copy(DateString,9,2));
except
result:=false;
exit;
end;
case month of
1: maxday:=31;
2: begin
if (year mod 4)=0 then maxday:=29 else maxday:=28;
end;
3: maxday:=31;
4: maxday:=30;
5: maxday:=31;
6: maxday:=30;
7: maxday:=31;
8: maxday:=31;
9: maxday:=30;
10: maxday:=31;
11: maxday:=30;
12: maxday:=31;
end;
if day>maxday then
begin
result:=false;
exit;
end;
end;
恺撒大帝引进的算法改进的。它采用的是闰年制也就是现行的制度,不过有一个需要注意的
地方就是,这个历法并不是连续的,中间缺少了11天。1752年9月2日之后的那一天并不是
1752年9月3日,而是1752年9月14日。也就是说,从1752年9月3日到1752年9月13日的11天
并不存在。抹掉这11天是由英国议会做出的决定。所以要计算某年每个月的天数的话,除了
要考虑是否是闰年以外,还要考虑1752年的9月。
程序可以这样:
if year=1752 then
case month of
1,3,5,7,8,10,12:days:=31;
2:days:=29;
9:days:=19;
4,6,11:days:=30;
end
else
case month of
1,3,5,7,8,10,12:days:=31;
4,6,9,11:days:=30;
2:if (year mod 400=0) or( (year mod 4=0) and (year mod 100<>))
then days:=29 else days:=28;
end;
恺撒大帝引进的算法改进的。它采用的是闰年制也就是现行的制度,不过有一个需要注意的
地方就是,这个历法并不是连续的,中间缺少了11天。1752年9月2日之后的那一天并不是
1752年9月3日,而是1752年9月14日。也就是说,从1752年9月3日到1752年9月13日的11天
并不存在。抹掉这11天是由英国议会做出的决定。所以要计算某年每个月的天数的话,除了
要考虑是否是闰年以外,还要考虑1752年的9月。
程序可以这样:
if year=1752 then
case month of
1,3,5,7,8,10,12:days:=31;
2:days:=29;
9:days:=19;
4,6,11:days:=30;
end
else
case month of
1,3,5,7,8,10,12:days:=31;
4,6,9,11:days:=30;
2:if (year mod 400=0) or( (year mod 4=0) and (year mod 100<>))
then days:=29 else days:=28;
end;