您的位置:首页 > 其它

计算某年的某月有多少天

2009-02-06 16:22 330 查看
今天有要求想返回一个月有多少天,在网上找了找,用以下的函数可以实现.

function DaysInAMonth(const AYear,AMonth:Word):Word; //for Delphi5
2 begin
3 Result:=MonthDays[(AMonth=2) and IsLeapYear(AYear),AMonth];
4 end;
5
6procedure TForm1.Button1Click(Sender: TObject);
7var
8 ss:Word;
9 yy,mm,dd:Word;
begin
DecodeDate(Now,yy,mm,dd);
ss:=DaysInAMonth(yy,mm);
ShowMessage(IntToStr(ss));

end;

比较可以学习和借鉴的地方是MonthDays 和IsLeapYear函数的实现:

{ The MonthDays array can be used to quickly find the number of
days in a month: MonthDays[IsLeapYear(Y), M] }

const
MonthDays: array [Boolean] of TDayTable =
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));

判断是否为润年:

function IsLeapYear(Year: Word): Boolean;
begin
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
end;
闰年的计算方法:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;

被100整除也可被400整除的为闰年。如2000年是闰年,而1900年不是。

判断是星期几:

function DayOfWeek(const DateTime: TDateTime): Word;
begin
Result := DateTimeToTimeStamp(DateTime).Date mod 7 + 1;
end;

{ Date and time record }

TTimeStamp = record
Time: Integer; { Number of milliseconds since midnight }
Date: Integer; { One plus number of days since 1/1/0001 }
end;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: