Get current year, day and month in delphi

I basically want this C# code:

if (month > currentDate.Month || (day > currentDate.Day && month > currentDate.Month)) 
{
    age = currentDate.Year - year - 1;
}
else
{
    age = currentDate.Year - year;
}

Into to delphi. But the problem I have is that I don’t know how to get the current year, month and day as integers so I can do some calculations. I’ve click on every result on google that relates to this but none of them have helped me.
(Really not a fan of delphi, but its the language we’re doing in my computing class)


3 Solutions
Solution

Add DateUtils to your uses, then you can use DayOf for the day of the month, MonthOf for the of the year and YearOf for the year part. Date gives you the current date to pass into these functions.

Solution

A literal translation would be as follows:

uses
  ..., DateUtils;

if (month > MonthOf(Date)) or ((day > DayOf(Date)) and (month > MonthOf(Date)) then 
begin
  age := YearOf(Date) - year - 1;
end else
begin
  age := YearOf(Date) - year;
end;

But that is the not the best translation. If nothing else, the repeated calls to Date() cause a problem if the code is running at midnight when the current date changes. The following would be safer:

uses
  ..., SysUtils;

var
  wYear, wMonth, wDay: Word:
begin
  ...
  DecodeDate(Date, wYear, wMonth, wDay);
  if (month > wMonth) or ((day > wDay) and (month > wMonth) then 
  begin
    age := wYear - year - 1;
  end else
  begin
    age := wYear - year;
  end;
  ...
end;

Alternatively, have a look at the DateUtils.YearsBetween() function:

uses
  ..., SysUtils, DateUtils;

age := YearsBetween(Date, EncodeDate(year, month, day));
Solution

You can use DecodeDate to separate out the year, month, and day values from the return value of the Date function in a single function call:

var
  CurrYear, CurrMonth, CurrDay: Word;
begin
  DecodeDate(Date(), CurrYear, CurrMonth, CurrDay);
  ...
end;