delphibuildnumbers

Delphi Days to Year, Month, Days convert


My IDE is RAD Studio 10.3.

My Project Setting in Version Info, "Auto Generate Build Number".

This Release number is 9004. Rad Studio Wiki (Release = number of days since Jan 1 2000) My project build 2024.08.26.

This is the code, but the date is not correct.

procedure TForm1.FormCreate(Sender: TObject);
var
  A, B, I, Y, R, M, Months, Days, o1, o2, o3: Integer;

begin
  A := 9004;
  B := 43904;

  Y := 2000;
  Days := 0;

  while Days < A do begin
    if (IsLeapYear(Y)) then begin
      Inc(Days,366);
    end Else begin
      Inc(Days,365);
    end;
    Inc(Y);
  end;
  Dec(Y);
  if (IsLeapYear(Y-1)) then
    Dec(Days,366)
  Else
    Dec(Days,365);
  R := A - Days;
  Months := 1;
  M := 0;
  while M < R do begin
    Inc(M,DaysInAMonth(Y,Months));
    Inc(Months);
  end;
  Dec(Months);
  Dec(M,DaysInAMonth(Y,Months));
  ShowMEssage(IntToStr(Y)+' - '+IntTOStr(Months)+' - '+IntToStr(R-M)); // 2024-08-24 Why ????    
end;

Solution

  • There are two errors in your code:

    1. The IsLeapYear(Y-1) should be IsLeapYear(Y), because you already decremented Y in the line before.
    2. In the last line (R-M) gives the day before the expected date, because the first day in a month is 1 and not 0. So (R-M+1) would be the correct term.

    However, I suggest to use the proper functions:

      var dt := IncDay(EncodeDate(2000, 1, 1), 9004);