unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Edit3: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ParserDate(const AFormatedDate: string;
var AYear, AMonth, ADay: Word);
function GetDayNum(const AFrom, ATo: string): Integer;
function IsLeapYear(AYear: Word): Boolean;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
{ 得到两个日期之间天数的算法 }
function TForm1.GetDayNum(const AFrom, ATo: string): Integer;
const
DayPerMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31);
DayPerMonthEx: array[1..12] of Integer = (31, 29, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31);
var
TmpFromYear, TmpFromMonth, TmpFromDay: Word;
TmpToYear, TmpToMonth, TmpToDay: Word;
i: Integer;
begin
{ 根据用户提供的数据格式解析 }
ParserDate(AFrom, TmpFromYear, TmpFromMonth, TmpFromDay);
ParserDate(ATo, TmpToYear, TmpToMonth, TmpToDay);
{ 判断两个日期的大小 }
if (TmpFromYear > TmpToYear) or
((TmpFromYear = TmpToYear) and (TmpFromMonth > TmpToMonth)) or
((TmpFromYear = TmpToYear) and (TmpFromMonth = TmpToMonth) and
(TmpFromDay > TmpToDay)) then
begin
ParserDate(AFrom, TmpToYear, TmpToMonth, TmpToDay);
ParserDate(ATo, TmpFromYear, TmpFromMonth, TmpFromDay);
end;
Result := 0;
{ 同一年的情况 }
if TmpFromYear = TmpToYear then
begin
if TmpFromMonth = TmpToMonth then
begin
Result := TmpToDay - TmpFromDay;
Exit;
end;
for i := TmpFromMonth + 1 to TmpToMonth - 1do
begin
if IsLeapYear(TmpFromYear) then
Result := Result + DayPerMonth
else
Result := Result + DayPerMonthEx;
end;
if IsLeapYear(TmpFromYear) then
Result := Result + DayPerMonth[TmpFromMonth] - TmpFromDay
else
Result := Result + DayPerMonthEx[TmpFromMonth] - TmpFromDay;
Result := Result + TmpToDay;
Exit;
end;
{ 计算整年的天数 }
for i := TmpFromYear + 1 to TmpToYear - 1do
begin
if IsLeapYear(i) then
Result := Result + 366
else
Result := Result + 365;
end;
{ 计算最先一年的天数 }
for i := TmpFromMonth + 1 to 12do
begin
if IsLeapYear(TmpFromYear) then
Result := Result + DayPerMonth
else
Result := Result + DayPerMonthEx;
end;
if IsLeapYear(TmpFromYear) then
Result := Result + DayPerMonth[TmpFromMonth] - TmpFromDay
else
Result := Result + DayPerMonthEx[TmpFromMonth] - TmpFromDay;
{ 计算最后一年的天数 }
for i := 1 to TmpToMonth - 1do
begin
if IsLeapYear(TmpToMonth) then
Result := Result + DayPerMonth
else
Result := Result + DayPerMonthEx;
end;
Result := Result + TmpToDay;
end;
{ 判断是否为闰年 }
function TForm1.IsLeapYear(AYear: Word): Boolean;
begin
Result := (AYear mod 4 = 0) and
((AYear mod 100 <> 0) or (AYear mod 400 = 0));
end;
{ 根据不同用户定义的格式解析日期 }
{ 比如提供日期为'19810115' }
procedure TForm1.ParserDate(const AFormatedDate: string;
var AYear, AMonth,
ADay: Word);
var
TmpStr: string;
begin
try
AYear := StrToInt(LeftStr(AFormatedDate, 4));
TmpStr := RightStr(AFormatedDate, 4);
AMonth := StrToInt(LeftStr(TmpStr, 2));
ADay := StrToInt(RightStr(TmpStr, 2));
except
AYear := 0;
AMonth := 0;
ADay := 0;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(GetDayNum(Edit1.Text, Edit2.Text)));
end;
end.