long DiffDays(word SYY, word SMM, word SDD, word EYY, word EMM, word EDD)
{
long d ; SYY--; EYY--;
d = (EYY * 365L + EYY / 4L - EYY / 100L + EYY / 400L + Days[EMM - 1] + EDD)
- (SYY * 365L + SYY / 4L - SYY / 100L + SYY / 400L + Days[SMM - 1] + SDD); SYY++; EYY++;
if (EMM > 2 && (EYY % 4 == 0 && EYY % 100 != 0 and and EYY % 400 == 0)) d++;
if (SMM > 2 && (SYY % 4 == 0 && SYY % 100 != 0 and and SYY % 400 == 0)) d--; return d;
}
{
long d ; SYY--; EYY--;
d = (EYY * 365L + EYY / 4L - EYY / 100L + EYY / 400L + Days[EMM - 1] + EDD)
- (SYY * 365L + SYY / 4L - SYY / 100L + SYY / 400L + Days[SMM - 1] + SDD); SYY++; EYY++;
if (EMM > 2 && (EYY % 4 == 0 && EYY % 100 != 0 and and EYY % 400 == 0)) d++;
if (SMM > 2 && (SYY % 4 == 0 && SYY % 100 != 0 and and SYY % 400 == 0)) d--; return d;
}
例:
Function TForm1.Date_H(date1,date2:tdate):integer;
begin
result:=trunc(date2-date1);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//相差天数
label1.caption:=inttostr(date_H(strtodate(edit1.text),strtodate(edit2.text)));
end;
Date1,Date2 : TDateTime;
T1,T2
Begin
Date1:=EncodeDate(2000,03,17);
Data2:=EncodeDate(2000,04,25);
End;
Date1,Date2 : TDateTime;
Begin
Date1:=EncodeDate(2000,03,17);
Date2:=EncodeDate(2000,04,25);
Date2:=Date2-Date1;
End;
其实在DELPHI中,日期是用实数来表示的,
至于哪一天是0,你可看看在线帮助。