Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to convert days into human-readable years, months and days in Pascal

2 Answers

0 votes
program DaysToYMD;

type
  TYMD = record
    Years  : Integer;
    Months : Integer;
    Days   : Integer;
  end;

function SplitDays(TotalDays : Integer) : TYMD;
begin
  SplitDays.Years  := TotalDays div 365;
  TotalDays        := TotalDays mod 365;

  SplitDays.Months := TotalDays div 30;
  TotalDays        := TotalDays mod 30;

  SplitDays.Days   := TotalDays;
end;

var
  R : TYMD;

begin
  R := SplitDays(452);

  WriteLn(R.Years, ' years, ', R.Months, ' months, ', R.Days, ' days');
end.




(*
run:

1 years, 2 months, 27 days

*)


 



answered Dec 31, 2025 by avibootz
0 votes
program DaysToCalendar;

// Calendar‑accurate (real month lengths)

type
  TYMD = record
    Years  : Integer;
    Months : Integer;
    Days   : Integer;
  end;

const
  MonthLen : array[1..12] of Integer =
    (31,28,31,30,31,30,31,31,30,31,30,31);

function IsLeap(Y : Integer) : Boolean;
begin
  IsLeap := (Y mod 4 = 0) and ((Y mod 100 <> 0) or (Y mod 400 = 0));
end;

function SplitDaysCalendar(TotalDays : LongInt) : TYMD;
var
  Y, M, ML : Integer;
begin
  Y := 1970;
  M := 1;

  { subtract whole years }
  while True do
  begin
    if IsLeap(Y) then ML := 366 else ML := 365;
    if TotalDays < ML then Break;
    Dec(TotalDays, ML);
    Inc(Y);
  end;

  { subtract whole months }
  while True do
  begin
    ML := MonthLen[M];
    if (M = 2) and IsLeap(Y) then Inc(ML); { February in leap year }

    if TotalDays < ML then Break;
    Dec(TotalDays, ML);
    Inc(M);
    if M > 12 then
    begin
      M := 1;
      Inc(Y);
    end;
  end;

  SplitDaysCalendar.Years  := Y - 1970;
  SplitDaysCalendar.Months := M - 1;
  SplitDaysCalendar.Days   := TotalDays;
end;

var
  R : TYMD;

begin
  R := SplitDaysCalendar(452);
  
  WriteLn(R.Years, ' years, ', R.Months, ' months, ', R.Days, ' days');
end.




(*
run:

1 years, 2 months, 28 days

*)


 



answered Dec 31, 2025 by avibootz

Related questions

...