DateTime

TDateTime type and timing methods.

The TDateTime is stored as a double with the integer part representing days and the fractional part being fraction of a day.

var
  d: TDateTime;

begin
  d := TDateTime.CreateFromSystem();
  WriteLn('The system date & time is: ', d.ToString());

  d := d.AddDays(1);
  WriteLn('Tomorrow at this moment it will be: ', d.ToString);

  // Create the date of 3rd of jan 2000 with time 8am
  d := TDateTime.Create(2000,1,2,3, 8,0,0,0);
  WriteLn('Year=', d.Year);
  WriteLn('Month=', d.Month);
  WriteLn('Day=', d.Day);
  WriteLn('Hour=', d.hour);

  // Change the year to 2020
  d.Year := 2020;

  WriteLn('Date: ', d.ToString('dd mm yyyy'));
  WriteLn('Time: ', d.ToString('hh ss'));
end;

TDateTime.Create

function TDateTime.Create(Year,Month,Week,Day: Integer; Hour,Min,Sec,MSec: Integer): TDateTime; static;

Create a TDateTime from scratch.


TDateTime.Create

function TDateTime.CreateFromSystem(): TDateTime; static;

Creates from the current system time.


TDateTime.CreateFromUnix

function TDateTime.CreateFromUnix(): TDateTime; static;

Create from current unix timestamp.


TDateTime.CreateFromISO

function TDateTime.CreateFromISO(Value: String): TDateTime; static;

Create from ISO 8601 string YYYYMMDDThhmmss and YYYY-MM-DD hh:mm:ss format

Examples:

  • 2022-08-30T13:45:38+0000

  • 2022-08-30T13:45:38

  • 20220830 134538


TDateTime.ToUnix

function TDateTime.ToUnix(IsUTC: Boolean = True): Int64;

Convert to unix time. IsUTC determines if Self is already UTC otherwise it will need to be converted.


TDateTime.ToISO

function TDateTime.ToISO(IsUTC: Boolean = True): String;

Convert to ISO 8601 string. IsUTC determines if Self is already UTC otherwise it will need to be converted.


TDateTime.ToString

function TDateTime.ToString(Fmt: String = 'c'): String;

Convert to a human like string. If fmt is unspecified c is used which will produce YYYY-MM-DD hh:mm:ss

  y      = Year last 2 digits
  yy     = Year last 2 digits
  yyyy   = Year as 4 digits
  m      = Month number no-leading 0
  mm     = Month number as 2 digits
  mmm    = Month using ShortDayNames (Jan)
  mmmm   = Month using LongDayNames (January)
  d      = Day number no-leading 0
  dd     = Day number as 2 digits
  ddd    = Day using ShortDayNames (Sun)
  dddd   = Day using LongDayNames  (Sunday)
  ddddd  = Day in ShortDateFormat
  dddddd = Day in LongDateFormat

  c     = Use ShortDateFormat + LongTimeFormat
  h     = Hour number no-leading 0
  hh    = Hour number as 2 digits
  n     = Minute number no-leading 0
  nn    = Minute number as 2 digits
  s     = Second number no-leading 0
  ss    = Second number as 2 digits
  z     = Milli-sec number no-leading 0s
  zzz   = Milli-sec number as 3 digits
  t     = Use ShortTimeFormat
  tt    = Use LongTimeFormat

  am/pm = use 12 hour clock and display am and pm accordingly
  a/p   = use 12 hour clock and display a and p accordingly

  /     = insert date separator
  :     = insert time separator
  "xx"  = literal text
  'xx'  = literal text
  [h]   = hours including the hours of the full days (i.e. can be > 24).
  [hh]  = hours with leading zero, including the hours of the full days (i.e. can be > 24)
  [n]   = minutes including the minutes of the full hours and days
  [nn]  = minutes with leading zero, including the minutes of the full hours and days
  [s]   = seconds including the seconds of the full minutes, hours and days.
  [ss]  = seconds with leading zero, including the seconds of the full minutes, hours and days.

TDateTime.AddYears

function TDateTime.AddYears(Amount: Integer = 1): TDateTime;

TDateTime.AddMonths

function TDateTime.AddMonths(Amount: Integer = 1): TDateTime;

TDateTime.AddDays

function TDateTime.AddDays(Amount: Integer = 1): TDateTime;

TDateTime.AddHours

function TDateTime.AddHours(Amount: Int64 = 1): TDateTime;

TDateTime.AddMinutes

function TDateTime.AddMinutes(Amount: Int64 = 1): TDateTime;

TDateTime.AddSeconds

function TDateTime.AddSeconds(Amount: Int64 = 1): TDateTime;

TDateTime.AddMilliseconds

function TDateTime.AddMilliseconds(Amount: Int64 = 1): TDateTime;

TDateTime.YearsBetween

function TDateTime.YearsBetween(Other: TDateTime): Integer;

TDateTime.MonthsBetween

function TDateTime.MonthsBetween(Other: TDateTime): Integer;

TDateTime.WeeksBetween

function TDateTime.WeeksBetween(Other: TDateTime): Integer;

TDateTime.DaysBetween

function TDateTime.DaysBetween(Other: TDateTime): Integer;

TDateTime.HoursBetween

function TDateTime.HoursBetween(Other: TDateTime): Int64;

TDateTime.MinutesBetween

function TDateTime.MinutesBetween(Other: TDateTime): Int64;

TDateTime.SecondsBetween

function TDateTime.SecondsBetween(Other: TDateTime): Int64;

TDateTime.MilliSecondsBetween

function TDateTime.MilliSecondsBetween(Other: TDateTime): Int64;

TDateTime.Date

property TDateTime.Date: TDateTime
property TDateTime.Date(NewValue: TDateTime)

Read or write just the date part.


TDateTime.Time

property TDateTime.Time: TDateTime
property TDateTime.Time(NewValue: TDateTime)

Read or write just the time part.


TDateTime.Year

property TDateTime.Year: Integer
property TDateTime.Year(NewValue: Integer)

TDateTime.Month

property TDateTime.Month: Integer
property TDateTime.Month(NewValue: Integer)

TDateTime.Day

property TDateTime.Day: Integer
property TDateTime.Day(NewValue: Integer)

TDateTime.Hour

property TDateTime.Hour: Integer
property TDateTime.Hour(NewValue: Integer)

TDateTime.Minute

property TDateTime.Minute: Integer
property TDateTime.Minute(NewValue: Integer)

TDateTime.Second

property TDateTime.Second: Integer
property TDateTime.Second(NewValue: Integer)

TDateTime.Millisecond

property TDateTime.Millisecond: Integer
property TDateTime.Millisecond(NewValue: Integer)

TDateTime.SystemTimeOffset

function TDateTime.SystemTimeOffset: Integer; static;

Returns the systems timezone offset in minutes. This is the difference between UTC time and local time.