每月最后一天(100分)

  • 主题发起人 主题发起人 dlmn
  • 开始时间 开始时间
function Tyuejie_form.GetYearMonthLastDate(Year, Month: Word): TDate;
var day:Word;
begin
CASE Month of
1,3,5,7,8,10,12: day := 31;
2:
if isLeapYear(Year) then
day := 29
else
day := 28;
4,6,9,11: day := 30;
else
Raise Exception.Create('Error: Month no in [1,12]');
end;
Result := EnCodeDate(Year, Month, day);
end;
正好刚遇到这个问题,这是我的笨方法,请指教
 
function TCalendar.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;
 
每月第N个星期X有吗?呵呵~~~

CRANE:见识,呵呵
 
//得到第几周的星期一和星期天的datetime值
//输入年nYear, 周数nWeekNO, 输出 星期一是那一天,星期天是那一天
procedure GetMyWeekDays(nYear, nWeekNo: integer; var dtMonday, dtSunday: TDatetime);
var
i ,j : integer;
nInitDays : integer; //今天是今年的第几天
nSurplusDays : integer;
nDaysInThismonth : integer;
begin
nInitDays :=(nWeekNo-1)*7+2-DayOftheWeek(StrToDatetime(IntToStr(nYear)+'-01-01'));

nSurplusDays :=nInitDays;
for i :=1 to 12 do
begin
nDaysInThismonth := DaysInMonth(StrToDatetime(IntToStr(nYear)+'-'+IntToStr(i)+'-01'));
if nSurplusDays > nDaysInThismonth then
nSurplusDays:=nSurplusDays - nDaysInThismonth
else
break;
end;

if nWeekNo>1 then //如果不是第一周
dtMOnday := StrToDatetime(IntToStr(nYear)+'-'+IntToStr(i)+'-'+IntToStr(nSurplusDays))
else //如果是第一周
dtMOnday := StrToDatetime(IntToStr(nYear)+'-01-01');

// showmessage('第'+intToStr(nNo)+'周的星期一是' + DatetimeToStr(dtMOnday));

nSurplusDays :=nInitDays+6;
for j :=1 to 12 do
begin
nDaysInThismonth := DaysInMonth(StrToDatetime(IntToStr(nYear)+'-'+IntToStr(j)+'-01'));
if nSurplusDays > nDaysInThismonth then
nSurplusDays:=nSurplusDays - DaysInMonth(StrToDatetime(IntToStr(nYear)+'-'+IntToStr(j)+'-01'))
else
break;
end;
dtSunday := StrToDatetime(IntToStr(nYear)+'-'+IntToStr(j)+'-'+IntToStr(nSurplusDays));
// showmessage('第'+intToStr(nNo)+'周的星期天是' + IntToStr(nYear)+'年'+IntToStr(j)+'月'+IntToStr(nSurplusDays)+'日');
end;
 
uses DateUtils
...
function EndOfAMonth(const AYear, AMonth: Word): TDateTime;
function EndOfTheMonth(const AValue: TDateTime): TDateTime;
 
没错!取出第一天,减一不就得到了一个月的最后一天吗?
 
后退
顶部