delphitdatetime

Function Now: Tdatetime for Perpetual Calendar


I am trying to write a function for Tdatetime (but when I open my program, he must show now data)

Below i have a function for TDatetime but I have an error in that:

((((''DecodeDateTime(data, rok, miesiac, dzien, godzina, minuta, sekunda, milisekunda);''))))

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Spin, Buttons;

type
  TForm1 = class(TForm)
    edRok: TSpinEdit;
    edMiesiac: TSpinEdit;
    edDzien: TSpinEdit;
    lbRok: TLabel;
    lbMiesiac: TLabel;
    lbDzien: TLabel;
    lbDT: TLabel;
    btnZamknij: TBitBtn;
    procedure edRokChange(Sender: TObject);
    procedure edDzienChange(Sender: TObject);
    procedure DecodeDate(Date: TDateTime);
    function DT(Dzien, Miesiac: Byte; Rok: Integer): Byte;
    function GetDzienText(Dt: Byte): String;
    function Now: TDateTime;



  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.edRokChange(Sender: TObject);
begin
 case edMiesiac.Value of
  1,3,5,7,8,10,12: edDzien.MaxValue:=31;
  4,6,9,11: edDzien.MaxValue:=30;
 else
  if edRok.Value mod 4=0 then edDzien.MaxValue:=29
  else edDzien.MaxValue:=28;
 end;{case}
 if edDzien.Value>edDzien.MaxValue then
  edDzien.Value:=edDzien.MaxValue;
  edDzienChange(Sender);
end;

procedure TForm1.edDzienChange(Sender: TObject);
begin
 lbDt.Caption:='Dzień tygodnia: '+GetDzienText(DT(eddzien.value, edmiesiac.value, edrok.value));
end;


procedure TForm1.DecodeDate(Date: TDateTime);
var
 Now, Rok, Miesiac, Dzien: Word;
begin
DecodeDate(Now);
DecodeDate(Rok);
DecodeDate(Miesiac);
DecodeDate(Dzien);
edRok.Value:=Rok;
edMiesiac.Value:=Miesiac;
edDzien.Value:=Dzien;
end;



function TForm1.DT(Dzien, Miesiac: Byte; Rok: Integer): Byte;
var m,r,w: Byte; {deklaracja zmiennych}
begin
 if Miesiac>2 then m:=miesiac-2
 else
  begin
   m:=miesiac+10;
   rok:=rok-1;
  end;
  r:=rok mod 100;
  w:=rok div 100;
 Result:=((13*m-1) div 5+dzien+r div 4+w div 4+r+5*w) mod 7;
end;

function TForm1.GetDzienText(Dt: Byte): String;
const Dni: array[0..6] of String=
      ('Niedziela','Poniedziałek','Wtorek','Środa',
       'Czwartek','Piątek','Sobota');
begin
 Result:=Dni[dt];
end;

function TForm1.Now: TDateTime;
var
  dzien, miesiac, rok: Word;
  godzina, minuta, sekunda, milisekunda: Word;
  data: TDateTime;

begin
  data := Now;
  DecodeDateTime(data, rok, miesiac, dzien, godzina, minuta, sekunda, milisekunda);
  MessageBox(0,PAnsiChar(Format('Data: %d/%d/%d %d:%d:%d:%d',[rok, miesiac, dzien, godzina, minuta, sekunda, milisekunda])), 'Data', MB_ICONINFORMATION);
end;

end.

Solution

  • You appear to have an infinite recursion together which will lead to a stack overflow (how fitting that you ask this question here).

    You could instead write:

    function TForm1.Now: TDateTime;
    var
      dzien, miesiac, rok: Word;
      godzina, minuta, sekunda, milisekunda: Word;
    begin
      Result := SysUtils.Now;
      SysUtils.DecodeDate(Result, rok, miesiac, dzien);
      DecodeTime(Result, godzina, minuta, sekunda, milisekunda);
      MessageBox(0,PChar(Format('Data: %d/%d/%d %d:%d:%d:%d',[rok, miesiac, dzien, godzina, minuta, sekunda, milisekunda])), 'Data', MB_ICONINFORMATION);
    end;
    

    I've had to fully qualify Now and DecodeDate because you use the same names in your code. You shouldn't really do that and should choose non-clashing names

    I don't really understand your aims and there are clearly many other problems in this code.


    Follow the comment trail of another answer I ran the following program:

    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      Windows, SysUtils;
    
    procedure Main;
    var
      dt: TDateTime;
      dzien, miesiac, rok: Word;
      godzina, minuta, sekunda, milisekunda: Word;
    begin
      dt := Now;
      DecodeDate(dt, rok, miesiac, dzien);
      DecodeTime(dt, godzina, minuta, sekunda, milisekunda);
      MessageBox(
        0,
        PChar(Format(
          'Data: %d/%d/%d %d:%d:%d:%d',
          [rok, miesiac, dzien, godzina, minuta, sekunda, milisekunda])
        ),
        'Data',
        MB_ICONINFORMATION
      );
    end;
    
    begin
      Main;
    end.
    

    while resulted in this output:

    alt text

    If your call to Now and DecodeDate really results in the year 2002, then I can only conclude that the time on your computer is wrong!