如何计算两个日期之间相差的月份, 是什么函数?(50分)

  • 主题发起人 主题发起人 krisjim
  • 开始时间 开始时间
K

krisjim

Unregistered / Unconfirmed
GUEST, unregistred user!
如何计算两个日期之间相差的月份, 是什么函数?
 
直接减就可以了
 
如果兩變量或常量都為日期型,那麼直接减就可以了,但結果好像還要進行一下處理
如果兩變旦或常量不為日期型,那麼你就先用strtodate函數進行一下轉換再减就可以了!
 
function MinuteSpan(const ANow, Athen
: TDateTime): do
uble;
Description
Call MinuteSpan to obtain the difference, in minutes, between two TDateTime values. Unlike the MinutesBetween function, which only counts entire minutes, MinuteSpan reports incomplete minutes as a fraction of an entire minute.
function MonthsBetween(const ANow, Athen
: TDateTime): Integer;
Description
Call MonthsBetween to obtain the difference, in months, between two TDateTime values. Because months are not all the same length, MonthsBetween returns an approximation based on an assumption of 30.4375 days per month. Fractional months are not counted. Thus, for example, MonthsBetween reports the difference between Feb 1 and Feb 28 as 0. (For that matter, it reports the difference between Feb 1 and Mar 1 as 0.)
 
可是得出的应该是天数, 而不是月份吧?
 
uses DateUtils;
procedure TForm1.Button1Click(Sender: TObject);
var
x,y:Tdate;
i:Double;
begin
x:=date;
y:=date-300;
i:=MonthsBetween(y,x);
showmessage(floattostr(i));
end;
 
呵呵,好像是这样的:
strToint(formatdatetime(DateX,'mm'))-strToint(formatdatetime(DateY,'mm'))
 
你可以这样做:!
function [:D]month_calcu(datetimepicker1,datetimepicker2:tdatetimepicker):integer;
var year1,year2,month1,month2:integer;
begin
month1:=strtoint(copy(datetimetostr(datetimepicker1.date),6,2));
month2:=strtoint(copy(datetimetostr(datetimepicker2.date),6,2));
year1:=strtoint(copy(datetimetostr(datetimepicker1.date),1,4));
year2:=strtoint(copy(datetimetostr(datetimepicker2.date),1,4));
result:=((year2 - year1)*12+(month2 - month1));
end;
 
用DateUtils的MonthsBetween或MonthSpan, 详细请看帮助!
 
month(data1)-month(data2)
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
D
回复
0
查看
911
DelphiTeacher的专栏
D
后退
顶部