计算某年的某月有多少天
2009-02-06 16:22
267 查看
今天有要求想返回一个月有多少天,在网上找了找,用以下的函数可以实现.
Code
1
function DaysInAMonth(const AYear,AMonth:Word):Word; //for Delphi5
2
begin
3
Result:=MonthDays[(AMonth=2) and IsLeapYear(AYear),AMonth];
4
end;
5
6
procedure TForm1.Button1Click(Sender: TObject);
7
var
8
ss:Word;
9
yy,mm,dd:Word;
10
begin
11
DecodeDate(Now,yy,mm,dd);
12
ss:=DaysInAMonth(yy,mm);
13
ShowMessage(IntToStr(ss));
14
15
end;
比较可以学习和借鉴的地方是MonthDays 和IsLeapYear函数的实现:
MonthDays
1
{ The MonthDays array can be used to quickly find the number of
2
days in a month: MonthDays[IsLeapYear(Y), M] }
3
4
const
5
MonthDays: array [Boolean] of TDayTable =
6
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
7
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
判断是否为润年:
IsLeapYear
1
function IsLeapYear(Year: Word): Boolean;
2
begin
3
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
4
end;
闰年的计算方法:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;
被100整除也可被400整除的为闰年。如2000年是闰年,而1900年不是。
判断是星期几:
DayOfWeek
1
function DayOfWeek(const DateTime: TDateTime): Word;
2
begin
3
Result := DateTimeToTimeStamp(DateTime).Date mod 7 + 1;
4
end;
TimeStamp
1
{ Date and time record }
2
3
TTimeStamp = record
4
Time: Integer; { Number of milliseconds since midnight }
5
Date: Integer; { One plus number of days since 1/1/0001 }
6
end;
Code
1
function DaysInAMonth(const AYear,AMonth:Word):Word; //for Delphi5
2
begin
3
Result:=MonthDays[(AMonth=2) and IsLeapYear(AYear),AMonth];
4
end;
5
6
procedure TForm1.Button1Click(Sender: TObject);
7
var
8
ss:Word;
9
yy,mm,dd:Word;
10
begin
11
DecodeDate(Now,yy,mm,dd);
12
ss:=DaysInAMonth(yy,mm);
13
ShowMessage(IntToStr(ss));
14
15
end;
比较可以学习和借鉴的地方是MonthDays 和IsLeapYear函数的实现:
MonthDays
1
{ The MonthDays array can be used to quickly find the number of
2
days in a month: MonthDays[IsLeapYear(Y), M] }
3
4
const
5
MonthDays: array [Boolean] of TDayTable =
6
((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
7
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31));
判断是否为润年:
IsLeapYear
1
function IsLeapYear(Year: Word): Boolean;
2
begin
3
Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
4
end;
闰年的计算方法:公元纪年的年数可以被四整除,即为闰年;被100整除而不能被400整除为平年;
被100整除也可被400整除的为闰年。如2000年是闰年,而1900年不是。
判断是星期几:
DayOfWeek
1
function DayOfWeek(const DateTime: TDateTime): Word;
2
begin
3
Result := DateTimeToTimeStamp(DateTime).Date mod 7 + 1;
4
end;
TimeStamp
1
{ Date and time record }
2
3
TTimeStamp = record
4
Time: Integer; { Number of milliseconds since midnight }
5
Date: Integer; { One plus number of days since 1/1/0001 }
6
end;
相关文章推荐
- 计算某年的某月有多少天
- Sql Server统计表中某年某月某个值有多少?
- ASP计算周开始和一年有多少周及某年第一周开始日期
- 计算某年某月的某一天是星期几的算法
- 从键盘上输入有效的年份和月份,显示某年的某月有多少天?
- 用javascript写的倒计时,从某年某月距离到今天还有多少时间
- 某年某月多少天函数
- Calendar GregorianCalendar 计算某年有多少周 、周的开始结束日期、日期的周数
- shell 求某年的某月有多少天
- 计算某年某月的某一天是星期几的算法
- javascript计算某年某月的天数
- 计算某年某月的某一天是星期几的算法
- 求用JAVA计算某年某月的天数?(两种方法)
- c++的万年历类,支持显示某月月历与某年年历,支持计算任意两个日期的间隔天数
- C#判断某年某月共有多少个周末,分别是哪几天
- 计算某年到某年之间相隔多少天(普通年有365天,润年有366天)
- PHP判断某年的某月有多少天
- 计算某年某月工作天数
- C++第三次作业(个人所得税计算器、某月有多少天、计算一个分段函数的值)
- 判断某年某月多少天.c