求助一个简单的算法得到两个日期的间隔天数。 我想算出两个日期中间有多少天,要求不管月大月小,每个月都按30天来算。我想写个简单又准确的过程,请大哥们指点。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Delphi自带函数:function DaysBetween(const ANow, AThen: TDateTime): Integer;描述:根据两个TDateTime类型的日期时间变量 ANow 和 AThen,DaysBetween函数能得到两者之间的天数的差距。DaysBetween 仅仅根据天数的不同来计算。因此,对于 1999年12月31日 下午11点59分 到2000年1月1日 11点58分,该函数得到的结果是 0,因为两者之间的时间差别还差 1 分钟才到1 天。 先算MonthsBetween,然后中间月*30,两头的月用30-掉日期,然后相加 用MonthsBetween,会将大月小月考虑进去,得到的月份有差别showmessage(inttostr(MonthsBetween(strToDate('2011-6-3'),strToDate('2011-4-3'))));结果是2showmessage(inttostr(MonthsBetween(strToDate('2011-6-3'),strToDate('2011-2-3'))));结果是3给你整个拆分计算的procedure TForm1.Button1Click(Sender: TObject);var dt1,dt2:TDateTime; month:word; year1,year2,month1,month2,day1,day2:word; allday:integer;begin dt1 := strToDate('2011-3-5'); dt2 := strToDate('2011-6-3'); decodedate(dt1,year1,month1,day1); decodedate(dt2,year2,month2,day2); //计算相差的月数 if month2>=month1 then month := (year2-year1)*12+(month2-month1) else month := (year2-year1-1)*12+(12-month1)+month2; //按月数计算的天数 allday := month*30; //考虑天数的差别 if day2>=day1 then allday := allday+(day2-day1) else allday := allday-(day1-day2); showmessage(inttostr(allday));end; 晕,这个你加个判断不就完了吗 dt1 := strToDate('2011-6-10'); dt2 := strToDate('2011-6-4'); if dt1>dt2 tehn begin tmpdt := dt1; dt1 := dt2; dt2 := tmpdt; end; 或者你限制好了就是了,一般后面的日期要大于前面的if dt1>dt2 thenbegin showmessage('结束日期应大于开始日期'); exit;end;一般日期相差多少天都是说的正的啊比如2011-06-10 和 2011-06-04 ,相差几天?反过来说2011-06-4 和 2011-06-10 ,相差几天?应该都说的正的 如何填充完整的BMP格式图 delphi操作字符串 请教如何使用快捷键? 请问Delphi中怎么运用TFrame? 在哪里能下载到SQL查询语言的电子书? 一个奇怪的问题!帮帮我 三层模式下一个插入数句库声音文件的问题? An error occurred while attemping to initialize the Borland Database Engine 超越“三急”的“四急”问题 用access开发的单机版软件怎样移到sqlserver的网络版 请教下Delphi怎么调用WebService啊 用delphi如何编程实现与outlook同步联系人
function DaysBetween(const ANow, AThen: TDateTime): Integer;描述:根据两个TDateTime类型的日期时间变量 ANow 和 AThen,DaysBetween函数能得到两者之间的
天数的差距。
DaysBetween 仅仅根据天数的不同来计算。因此,对于 1999年12月31日 下午11点59分 到
2000年1月1日 11点58分,该函数得到的结果是 0,因为两者之间的时间差别还差 1 分钟才到
1 天。
showmessage(inttostr(MonthsBetween(strToDate('2011-6-3'),strToDate('2011-4-3'))));
结果是2
showmessage(inttostr(MonthsBetween(strToDate('2011-6-3'),strToDate('2011-2-3'))));
结果是3给你整个拆分计算的
procedure TForm1.Button1Click(Sender: TObject);
var
dt1,dt2:TDateTime;
month:word;
year1,year2,month1,month2,day1,day2:word;
allday:integer;
begin
dt1 := strToDate('2011-3-5');
dt2 := strToDate('2011-6-3'); decodedate(dt1,year1,month1,day1);
decodedate(dt2,year2,month2,day2);
//计算相差的月数
if month2>=month1 then
month := (year2-year1)*12+(month2-month1)
else
month := (year2-year1-1)*12+(12-month1)+month2;
//按月数计算的天数
allday := month*30;
//考虑天数的差别
if day2>=day1 then
allday := allday+(day2-day1)
else
allday := allday-(day1-day2); showmessage(inttostr(allday));
end;
dt2 := strToDate('2011-6-4'); if dt1>dt2 tehn
begin
tmpdt := dt1;
dt1 := dt2;
dt2 := tmpdt;
end;
if dt1>dt2 then
begin
showmessage('结束日期应大于开始日期');
exit;
end;
一般日期相差多少天都是说的正的啊
比如2011-06-10 和 2011-06-04 ,相差几天?
反过来说2011-06-4 和 2011-06-10 ,相差几天?
应该都说的正的