var i:integer; begin I:=DaysBetween(date1,date2);//I为date1,date2之间的天数 DecodeDate(date1, YY,MM,DD); if I=MonthDay(YY,MM) then //就是整月 else //不是整月 end;//计算每月天数 FUNCTION MonthDay(YY,MM:integer):integer; begin CASE MM OF 1,3,5,7,8,10,12: Result:=31; 4,6,9,11: Result:=30; 2: IF ISLEAPYEAR(YY) THEN Result:=29 ELSE Result:=28; END; end;
function MonthsBetween(const ANow, AThen: TDateTime): Integer;
DecodeDate(DateTime1.Date, MyYear2, MyMonth2, MyDay2);
先分解,在相减,判断
DecodeDate(DateTime.Date, MyYear1, MyMonth1, MyDay1);
DecodeDate(DateTime1.Date, MyYear2, MyMonth2, MyDay2);
if (mymonth1<>mymonth2) and (myday1=myday2) then
相差整月
else
…
showmessage('相差整月')
用MonthsBetween函数计算出错,例如2003-2-1到2003-3-1之间的月份数应该是1但是MonthsBetween计算出来是0,看一下源代码就知道问题出在哪儿了!我有一个很笨的办法,计算是没有问题:
//获取两个时间之间的月份数
function MonthBetween(DateFm, DateTo: TDateTime): Integer;
begin
Result:=(StrToInt(FormatDateTime('yyyy',DateTo))-StrToInt(FormatDateTime('yyyy',DateFm)))*12+StrToInt(FormatDateTime('m',DateTo))-StrToInt(FormatDateTime('m',DateFm));
end;
2003-6-30 到 2003-7-1之间可以说相差一个月,可以说相差0个月,完全看你的统计方法而言!上面提到的方法得出的结果是1个月,而用DateUtils中的MonthsBetween得出的结果是0个月
begin
I:=DaysBetween(date1,date2);//I为date1,date2之间的天数
DecodeDate(date1, YY,MM,DD);
if I=MonthDay(YY,MM) then
//就是整月
else
//不是整月
end;//计算每月天数
FUNCTION MonthDay(YY,MM:integer):integer;
begin
CASE MM OF
1,3,5,7,8,10,12:
Result:=31;
4,6,9,11:
Result:=30;
2:
IF ISLEAPYEAR(YY) THEN
Result:=29
ELSE
Result:=28;
END;
end;