function IsLeapYear(AYear: Integer): Boolean; begin Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or (AYear mod 400 = 0)); end;function DaysPerMonth(AYear, AMonth: Integer): Integer; const DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); begin Result := DaysInMonth[AMonth]; if (AMonth = 2) and IsLeapYear(AYear) then Inc(Result); { leap-year Feb is special } end;
下面的函数返回指定日期的所在月的最后一天的日期: function LastDayOfMonth(Dat: TDate): TDate;varD, M, Y : Word;beginDecodeDate(IncMonth(Dat, 1), Y, M, D);Result := EncodeDate(Y, M, 1) - 1;end;
delphi上本身没有这样的函数。 本站有好多这样对日期的处理函数,看 看吧
use DateUtils;function DaysInAMonth(const AYear, AMonth: Word): Word;如: showmessage(inttostr(DaysInAMonth(2003,4)));
// 取得本月的最后一天 function LastDayOfCurrentMonth: TDate; var Y, M, D: Word; begin DecodeDate(Now, Y, M, D); Inc(M); if M > 12 then begin Y := Y + 1; M := 1; end; Result := EncodeDate(Y, M, 1) - 1; end;其它月类似!
begin
Result := (AYear mod 4 = 0) and ((AYear mod 100 <> 0) or (AYear mod 400 = 0));
end;function DaysPerMonth(AYear, AMonth: Integer): Integer;
const
DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
Result := DaysInMonth[AMonth];
if (AMonth = 2) and IsLeapYear(AYear) then Inc(Result); { leap-year Feb is special }
end;
function LastDayOfMonth(Dat: TDate): TDate;varD, M, Y : Word;beginDecodeDate(IncMonth(Dat, 1), Y, M, D);Result := EncodeDate(Y, M, 1) - 1;end;
本站有好多这样对日期的处理函数,看 看吧
showmessage(inttostr(DaysInAMonth(2003,4)));
将时间设为下一月的1号,再将此日期-1就是此月的最后一天
------就这样做,
function LastDayOfCurrentMonth: TDate;
var
Y, M, D: Word;
begin
DecodeDate(Now, Y, M, D);
Inc(M);
if M > 12 then begin
Y := Y + 1;
M := 1;
end;
Result := EncodeDate(Y, M, 1) - 1;
end;其它月类似!