📄 date.pas
字号:
unit Date;interfaceuses SysUtils;type TMonthType = 1 .. 12; TDayType = 1 .. 31;
TMyDate = class( TObject )
private
{ Private declarations }
FYear: Integer;
FMonth: TMonthType;
FDay: TDayType;
public
{ Public declarations }
constructor Create(); overload;
constructor Create(
AYear: Integer; AMonth: TMonthType; ADay: TDayType ); overload;
function IsLeapYear(): Boolean;
procedure SetDate(
AYear: Integer; AMonth: TMonthType; ADay: TDayType );
procedure GetDate(
var AYear: Integer; var AMonth: TMonthType; var ADay: TDayType );
function GetDateAsText(): string;
procedure Inc();
procedure Dec();
destructor Destroy(); override;
end;
implementationconstructor TMyDate.Create();
begin
FYear := 2000;
FMonth := 1;
FDay := 1;
end;
constructor TMyDate.Create(
AYear: Integer; AMonth: TMonthType; ADay: TDayType );
begin
FYear := AYear;
FMonth := AMonth;
FDay := ADay;
end;
function TMyDate.IsLeapYear(): Boolean;
begin
if ( FYear mod 4 <> 0 ) then
IsLeapYear := false
else if ( FYear mod 100 <> 0 ) then
IsLeapYear := true
else if ( FYear mod 400 <>0 ) then
IsLeapYear := false
else
IsLeapYear := true;
end;
procedure TMyDate.SetDate(
AYear: Integer; AMonth: TMonthType; ADay: TDayType );
begin
FYear := AYear;
FMonth := AMonth;
FDay := ADay;
end;
procedure TMyDate.GetDate(
var AYear: Integer; var AMonth: TMonthType; var ADay: TDayType );
begin
AYear := FYear;
AMonth := FMonth;
ADay := FDay;
end;
function TMyDate.GetDateAsText(): string;
begin
Result := Format( '%4.4d%2.2d%2.2d', [ FYear, FMonth, FDay ] );
end;
procedure TMyDate.Inc();
begin
case FMonth of
1, 3, 5, 7, 8, 10:
begin
if FDay < 31 then
System.Inc( FDay )
else
begin
FDay := 1;
System.Inc( FMonth );
end;
end;
4, 6, 9, 11:
begin
if FDay < 30 then
System.Inc( FDay )
else
begin
FDay := 1;
System.Inc( FMonth );
end;
end;
2:
begin
if IsLeapYear() then
begin
if FDay < 29 then
System.Inc( FDay )
else
begin
FDay := 1;
System.Inc( FMonth );
end;
end
else
begin
if FDay < 28 then
System.Inc( FDay )
else
begin
FDay := 1;
System.Inc( FMonth );
end;
end;
end;
12:
begin
if FDay < 31 then
System.Inc( FDay )
else
begin
FDay := 1;
FMonth := 1;
System.Inc( FYear );
end;
end;
end;
end;
procedure TMyDate.Dec();
begin
case FMonth of
1:
begin
if FDay > 1 then
System.Dec( FDay )
else
begin
FDay := 31;
FMonth := 12;
System.Dec( FYear );
end;
end;
2, 4, 6, 8, 9, 11:
begin
if FDay > 1 then
System.Dec( FDay )
else
begin
FDay := 31;
System.Dec( FMonth );
end;
end;
3:
begin
if IsLeapYear() then
begin
if FDay > 1 then
System.Dec( FDay )
else
begin
FDay := 29;
System.Dec( FMonth );
end;
end
else
begin
if FDay > 1 then
System.Dec( FDay )
else
begin
FDay := 28;
System.Dec( FMonth );
end;
end;
end;
5, 7, 10, 12:
begin
if FDay > 1 then
System.Dec( FDay )
else
begin
FDay := 30;
System.Dec( FMonth );
end;
end;
end;
end;
destructor TMyDate.Destroy();
begin
inherited Destroy();
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -