特急:求一返回两Tdatetime之间天数的函数。(100分)

  • 主题发起人 主题发起人 bluppy
  • 开始时间 开始时间
B

bluppy

Unregistered / Unconfirmed
GUEST, unregistred user!
求一返回两Tdatetime之间天数的函数。
 
uses
DateUtils;

DaysBetween
 
var D:Double;
data1,data2:Tdatetime;
old:string;
begin
old:='2002-09-10 12:30:00';
data1:=strtodatetime(old);
data2:=now;
d:=data2-data1;
showmessage(floattostr(d));
end;
 
var
year,month,day,year1,month1,day1:word;
begin
decodedate(time1,year,month,day);
decodedate(time2,year1,month1,day1);
showmessage(inttostr(day1-day));
end;
 
LeeChange说的DaysBetween比较简单且正规(专门用于日期间隔计算)、
 
呵呵~~~~~~~收回我错误的发言!支持LeeChange!
 
to bjaman
我的D6有这个单元!
 
DateUtils
function DaySpan(const ANow, AThen: TDateTime): Double;

Description

Call DaySpan to obtain the difference, in days, between two TDateTime values. Unlike the DaysBetween function, which only counts whole days, DaySpan reports incomplete days as a fraction of an entire day.
 
直接相减就可以得到啦。
 
我的D6也有,這樣的貼子,快點結了就算了。LeeChange就是答案了。不過DaySpan也可以,我還沒有認真弄清哪個打哪個,只是知道有一些區別。
 
kouchun说的行好像不通哦,Decodedate 只是将Date 分解成年、月、日,怎么能相减哩??
 
DaysBetween 是两个 DateTime 之间的天数(整数),即便两个 DateTime 只差1秒就是24小时,返回的结果仍然是 0。DaySpan 则包含小数部分。
kouchun 的方法,如果两个 DateTime 在同一个月,结果是正确的(要注意的是两个 DateTime,一个比另一个多一秒,只要是跨天,所得的结果也是 1 天。)如果不在同一个月,结果就是错误的。
 
引用:
先 uses uses dateutils;
来自:tianhf (2002-9-6 7:49)
如果你使用delphi6,用DaysBetween:
function DaysBetween(const ANow, AThen: TDateTime): Integer;

Description
Call DaysBetween to obtain the difference, in days, between two TDateTime values.
DaysBetween counts only whole days that have elapsed. Thus, DaysBetween reports the
difference between Dec 31, 1999 11:59 PM and Jan 1, 2000 11:58 PM as 0 because the
difference is one minute short of an entire day.
 
再提供一种方法:
floattostr(round(DateTimePicker2.date-DateTimePicker1.date))//也可以正确得到天数。
 
整数部分取出来直接减
 
DATE:TDATETIME;
DATE:=now;
然后分解DATE
想要什么类型都可以;
 
那么怎么才能同时判断Tdatetime的大小?
 
我有,请楼主给分吧。昨天还在写的:此函数来自于杜宝大俠,我只是进行了一些修改。

function TFun.DateTimeDiff(St, Et: TDateTime): string;
var
SYear, SMonth, SDay, SHour, SMin, SSec, SMSec: Word
// ¿ªÊ¼Ê±¼ä
EYear, EMonth, EDay, EHour, EMin, ESec, EMSec: Word
// ½áÊøʱ¼ä
DYear, DMonth, DDay, DHour, DMin, DSec, DMSec: Integer
// ʱ²î
EDate: TDateTime
// Öмä±äÁ¿
{ i: Integer
// Öмä±äÁ¿£¬¼Ç¼ʱ²î¼ä¸ô¡£}
Y, M, D, H, N, S {, MS}: Word;
procedure InitVars;
begin
DecodeDateTime(EDate, EYear, EMonth, EDay, EHour, EMin, ESec, EMSec);
end;

begin
if St > Et then // ²ÎÊý³ö´í
begin
EDate := St
// ½»»»Æðֹʱ¼ä
St := Et;
Et := EDate;
end;

// ±äÁ¿³õʼ»¯¡£
DecodeDateTime(St, SYear, SMonth, SDay, SHour, SMin, SSec, SMSec);
EDate := Et;
InitVars;

// ½øλµÄ¼Ó¼õ£¬²»×ãÔò½èλ¡£
// ´Ó×îµÍλ¿ªÊ¼£¬ÏÈÀ´ºÁÃëλ£º
DMSec := EMSec - SMSec
// È¡ºÁÃëλ²î
if DMSec < 0 then
begin
DMSec := 1000 + DMSec;
EDate := IncSecond(EDate, -1)
// &amp;frac12;è&amp;Icirc;&amp;raquo;
InitVars
// &amp;cedil;&amp;Auml;±&amp;auml;&amp;Aacute;&amp;iquest;
end;

DSec := ESec - SSec
// &amp;Egrave;&amp;iexcl;&amp;Atilde;&amp;euml;&amp;Icirc;&amp;raquo;&amp;sup2;&amp;icirc;
if DSec < 0 then
begin
DSec := 60 + DSec;
EDate := IncMinute(EDate, -1);
InitVars;
end;

DMin := EMin - SMin
// &amp;Egrave;&amp;iexcl;·&amp;Ouml;&amp;Ouml;&amp;Oacute;&amp;sup2;&amp;icirc;
if DMin < 0 then
begin
DMin := 60 + DMin;
EDate := IncHour(EDate, -1);
InitVars;
end;

DHour := EHour - SHour
// &amp;Egrave;&amp;iexcl;&amp;ETH;&amp;iexcl;&amp;Ecirc;±&amp;sup2;&amp;icirc;
if DHour < 0 then
begin
DHour := 24 + DHour;
EDate := IncDay(EDate, -1);
InitVars;
end;

DDay := EDay - SDay
// &amp;Egrave;&amp;iexcl;&amp;Igrave;ì&amp;sup2;&amp;icirc;&amp;pound;&amp;not;&amp;Otilde;&amp;acirc;&amp;Agrave;&amp;iuml;&amp;frac34;&amp;Iacute;&amp;cedil;&amp;acute;&amp;Ocirc;&amp;Oacute;&amp;Aacute;&amp;Euml;&amp;pound;&amp;iexcl;
if DDay < 0 then
begin
DDay := DaysInMonth(SDay) - SDay + EDay
// &amp;micro;&amp;frac12;&amp;Ocirc;&amp;Acirc;&amp;micro;×&amp;Ocirc;&amp;Ugrave;&amp;frac14;&amp;Oacute;
EDate := IncMonth(EDate, -1);
InitVars;
end;

DMonth := EMonth - SMonth
// &amp;Egrave;&amp;iexcl;&amp;Ocirc;&amp;Acirc;&amp;sup2;&amp;icirc;
if DMonth < 0 then
begin
DMonth := 12 + DMonth;
EDate := IncYear(EDate, -1);
InitVars;
end;

DYear := EYear - SYear
// &amp;Egrave;&amp;iexcl;&amp;Auml;ê&amp;sup2;&amp;icirc;
Assert(DYear >= 0, '&amp;Oacute;&amp;brvbar;&amp;cedil;&amp;Atilde;&amp;sup2;&amp;raquo;&amp;iquest;&amp;Eacute;&amp;Auml;&amp;Uuml;&amp;ETH;&amp;iexcl;&amp;Oacute;&amp;Uacute;0&amp;pound;&amp;iexcl;');

Y := DYear
M := DMonth
D := DDay;
H := DHour
N := DMin
S := DSec
{ MS := DMSec;}
if Y > 0 then Result := IntToStr(Y) + '&amp;Auml;ê'
else if M > 0 then Result := IntToStr(M) + '&amp;Ocirc;&amp;Acirc;'
else if D > 0 then Result := IntToStr(D) + '&amp;Igrave;ì'
else if H > 0 then Result := IntToStr(H) + '&amp;ETH;&amp;iexcl;&amp;Ecirc;±'
else if N > 0 then Result := IntToStr(N) + '·&amp;Ouml;&amp;Ouml;&amp;Oacute;'
else Result := IntToStr(S) + '&amp;Atilde;&amp;euml;';
end;

{-----------------------------------------------------------------------------
原来的函数
-----------------------------------------------------------------------------}
function TFun.De2DateTime(const St, Et: TDateTime): string;
const
Fmt = 相差%d 年 %d 月 %d 日 %d 小时 %d 分%d 秒 %d 毫秒';
var
//&amp;Ograve;&amp;Ocirc;&amp;Iuml;&amp;Acirc;±&amp;auml;&amp;Aacute;&amp;iquest;&amp;pound;&amp;not;S&amp;iquest;&amp;ordf;&amp;Ecirc;&amp;frac14;&amp;micro;&amp;Auml;&amp;Icirc;&amp;ordf;&amp;iquest;&amp;ordf;&amp;Ecirc;&amp;frac14;&amp;Ecirc;±&amp;frac14;&amp;auml;&amp;pound;&amp;not;E&amp;iquest;&amp;ordf;&amp;Ecirc;&amp;frac14;&amp;micro;&amp;Auml;&amp;Icirc;&amp;ordf;&amp;frac12;á&amp;Ecirc;&amp;oslash;&amp;Ecirc;±&amp;frac14;&amp;auml;&amp;iexcl;&amp;pound;
SYear, EYear, SMonth, EMonth, SDay, EDay: Word;
SHour, EHour, SMin, EMin, SSec, ESec, SMSec, EMSec: Word;

EDate: TDateTime
//&amp;AElig;&amp;eth;&amp;iexcl;&amp;cent;&amp;Ouml;&amp;Aacute;&amp;Ecirc;&amp;yacute;&amp;frac34;&amp;Yacute;&amp;pound;&amp;not;&amp;Ouml;&amp;raquo;&amp;Ecirc;&amp;Ccedil;&amp;acute;&amp;laquo;&amp;Egrave;&amp;euml;&amp;sup2;&amp;Icirc;&amp;Ecirc;&amp;yacute;&amp;sup2;&amp;raquo;&amp;Ecirc;&amp;Ccedil;Var&amp;micro;&amp;Auml;&amp;pound;&amp;not;×&amp;ouml;&amp;Ouml;&amp;ETH;&amp;frac14;&amp;auml;±&amp;auml;&amp;Aacute;&amp;iquest;&amp;Oacute;&amp;Atilde;&amp;iexcl;&amp;pound;
DYear, DMonth, DDay, DHour, DMin, DSec, DMSec: Integer
//&amp;Ecirc;±&amp;sup2;&amp;icirc;&amp;iexcl;&amp;pound;
I: Integer
//&amp;Ouml;&amp;ETH;&amp;frac14;&amp;auml;±&amp;auml;&amp;Aacute;&amp;iquest;&amp;pound;&amp;not;&amp;frac14;&amp;Ccedil;&amp;Acirc;&amp;frac14;&amp;Ecirc;±&amp;sup2;&amp;icirc;&amp;frac14;&amp;auml;&amp;cedil;&amp;ocirc;&amp;iexcl;&amp;pound;

//&amp;Ouml;&amp;ETH;&amp;frac14;&amp;auml;&amp;sup1;&amp;yacute;&amp;sup3;&amp;Igrave;&amp;pound;&amp;not;&amp;sup3;&amp;otilde;&amp;Ecirc;&amp;frac14;±&amp;auml;&amp;Aacute;&amp;iquest;&amp;iexcl;&amp;pound;
procedure InitVars;
begin
DeCodeDate(EDate, EYear, EMonth, EDay);
DeCodeTime(EDate, EHour, EMin, ESec, EMSec);
end;

begin
if St > Et then //&amp;sup2;&amp;Icirc;&amp;Ecirc;&amp;yacute;&amp;sup3;&amp;ouml;&amp;acute;í&amp;iexcl;&amp;pound;
begin
Result := 'Error!';
Exit;
end;
{ ±&amp;auml;&amp;Aacute;&amp;iquest;&amp;sup3;&amp;otilde;&amp;Ecirc;&amp;frac14;&amp;raquo;&amp;macr;&amp;iexcl;&amp;pound;
DYear:=0 ;DMonth:=0;DDay:=0;DHour:=0;DMin:=0;DSec:=0;DMSec:=0;}
EDate := Et;
DeCodeDate(St, SYear, SMonth, SDay);
DeCodeTime(St, SHour, SMin, SSec, SMSec);

InitVars;
//&amp;frac12;&amp;oslash;&amp;Icirc;&amp;raquo;&amp;micro;&amp;Auml;&amp;frac14;&amp;Oacute;&amp;frac14;&amp;otilde;&amp;iexcl;&amp;pound;&amp;acute;&amp;Oacute;×&amp;icirc;&amp;micro;&amp;Iacute;&amp;Icirc;&amp;raquo;&amp;iquest;&amp;ordf;&amp;Ecirc;&amp;frac14;&amp;pound;&amp;iexcl;
//&amp;Iuml;&amp;Egrave;&amp;Agrave;&amp;acute;&amp;ordm;&amp;Aacute;&amp;Atilde;&amp;euml;&amp;Icirc;&amp;raquo;&amp;pound;&amp;ordm;

DMSec := EMSec - SMSec
//&amp;Egrave;&amp;iexcl;&amp;ordm;&amp;Aacute;&amp;Atilde;&amp;euml;&amp;Icirc;&amp;raquo;&amp;sup2;&amp;icirc;
if DMSec < 0 then //&amp;sup2;&amp;raquo;×&amp;atilde;&amp;Oacute;&amp;brvbar;&amp;cedil;&amp;Atilde;&amp;frac12;è&amp;Icirc;&amp;raquo;
begin
DMSec := 1000 + DMSec;
EDate := IncSecond(EDate, -1)
//&amp;frac12;è&amp;Icirc;&amp;raquo;&amp;iexcl;&amp;pound;
InitVars
//&amp;cedil;&amp;Auml;±&amp;auml;&amp;Aacute;&amp;iquest;&amp;iexcl;&amp;pound;
end;

DSec := ESec - SSec
//&amp;Egrave;&amp;iexcl;&amp;Atilde;&amp;euml;&amp;Icirc;&amp;raquo;&amp;sup2;&amp;icirc;
if DSec < 0 then
begin
DSec := 60 + DSec;
EDate := IncMinute(EDate, -1);
InitVars;
end;

DMin := EMin - SMin
//&amp;Egrave;&amp;iexcl;·&amp;Ouml;&amp;Ouml;&amp;Oacute;&amp;sup2;&amp;icirc;
if DMin < 0 then
begin
DMin := 60 + DMin;
EDate := IncHour(EDate, -1);
InitVars;
end;

DHour := EHour - SHour
//&amp;Egrave;&amp;iexcl;&amp;ETH;&amp;iexcl;&amp;Ecirc;±&amp;sup2;&amp;icirc;
if DHour < 0 then
begin
DHour := 24 + DHour;
EDate := IncDay(EDate, -1);
InitVars;
end;
//**********************************************
//&amp;cedil;&amp;Auml;&amp;pound;&amp;ordm;
DDay := EDay - SDay
//&amp;Egrave;&amp;iexcl;&amp;Igrave;ì&amp;sup2;&amp;icirc;&amp;pound;&amp;not;&amp;Otilde;&amp;acirc;&amp;Agrave;&amp;iuml;&amp;frac34;&amp;Iacute;&amp;cedil;&amp;acute;&amp;Ocirc;&amp;Oacute;&amp;Aacute;&amp;Euml;&amp;pound;&amp;iexcl;
if DDay < 0 then
begin
DDay := DaysInMonth(SDay) - SDay + EDay
//&amp;micro;&amp;frac12;&amp;Ocirc;&amp;Acirc;&amp;micro;×&amp;pound;&amp;not;&amp;Ocirc;&amp;Ugrave;&amp;frac14;&amp;Oacute;
EDate := IncMonth(EDate, -1);
InitVars;
end;
{ &amp;Ocirc;&amp;shy;&amp;Iuml;&amp;Egrave;&amp;micro;&amp;Auml;&amp;Ecirc;&amp;Ccedil;&amp;acute;í&amp;micro;&amp;Auml;
******************************************
DDay := EDay - SDay;//&amp;Egrave;&amp;iexcl;&amp;Igrave;ì&amp;sup2;&amp;icirc;&amp;pound;&amp;not;&amp;Otilde;&amp;acirc;&amp;Agrave;&amp;iuml;&amp;frac34;&amp;Iacute;&amp;cedil;&amp;acute;&amp;Ocirc;&amp;Oacute;&amp;Aacute;&amp;Euml;&amp;pound;&amp;iexcl;
if DDay < 0 then
begin
while DDay < 0 do
begin
i:= DaysBetween(EDate , IncMonth(EDate,-1));//&amp;Egrave;&amp;iexcl;&amp;micro;±&amp;Ocirc;&amp;Acirc;&amp;Igrave;ì&amp;Ecirc;&amp;yacute;
DDay := i + DDay;
EDate :=IncMonth(EDate,-1);
end;
InitVars;
end;
***************************************
}
DMonth := EMonth - SMonth;
if DMonth < 0 then
begin
DMonth := 12 + DMonth;
EDate := IncYear(EDate, -1);
InitVars;
end;

DYear := EYear - SYear;
if DYear < 0 then
begin
Result := 'Error!';
Exit;
end;

Result := Format(Fmt, [DYear, DMonth, DDay, DHour, DMin, DSec, DMSec]);
end;

注意一下,乱码是注释用的中文。你可以直接复制到编辑器,去掉了就行了。
 
多人接受答案了。
 
后退
顶部