Pascal Tips Homepage  •  Privacy Policy & Imprint

FreePascal Unit SysUtils

Date/Time Routines

Reference of the date and time handling functions from the SysUtils unit, section Date/Time Routines.
Every function name links to its original documentation page.

DateTimeToFileDate

function DateTimeToFileDate(DateTime: TDateTime): LongInt;

Converts a TDateTime value to a DOS-style file date/time integer, of the kind returned by functions such as FileAge.

DateTimeToStr

function DateTimeToStr(DateTime: TDateTime; ForceTimeIfZero: Boolean = False): string;
function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings; ForceTimeIfZero: Boolean = False): string;

Returns a string representation of DateTime using the ShortDateFormat and LongTimeFormat settings, equivalent to FormatDateTime('c', DateTime); by default the time part is omitted when it is exactly midnight, unless ForceTimeIfZero is set to True.

DateTimeToString

procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; Options: TFormatDateTimeOptions = []);
procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings; Options: TFormatDateTimeOptions = []);

Writes a string representation of DateTime into Result, formatted according to the formatting characters given in FormatStr; raises EConvertError if FormatStr contains an invalid formatting character.

DateTimeToSystemTime

procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);

Converts a date/time value in TDateTime format into a TSystemTime record, breaking it out into year, month, day, hour, minute, second, and millisecond fields.

DateTimeToTimeStamp

function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;

Converts a TDateTime value to a TTimeStamp record, which represents the date as a day count and the time as milliseconds since midnight.

DateToStr

function DateToStr(Date: TDateTime): string;
function DateToStr(Date: TDateTime; const FormatSettings: TFormatSettings): string;

Converts Date to a string using the ShortDateFormat setting, equivalent to FormatDateTime('ddddd', Date).

Date

function Date: TDateTime;

Returns the current date in TDateTime format, with the time portion set to zero.

DayOfWeek

function DayOfWeek(DateTime: TDateTime): Integer;

Returns the day of the week for DateTime, counting Sunday as day 1 and Saturday as day 7; the result can be used as an index into the LongDayNames constant array.

DecodeDate

procedure DecodeDate(Date: TDateTime; out Year: Word; out Month: Word; out Day: Word);

Splits the Year, Month and Day encoded in Date into the corresponding output parameters.

DecodeTime

procedure DecodeTime(Time: TDateTime; out Hour: Word; out Minute: Word; out Second: Word; out MilliSecond: Word);

Splits the Hour, Minute, Second and Millisecond encoded in Time into the corresponding output parameters.

EncodeDate

function EncodeDate(Year: Word; Month: Word; Day: Word): TDateTime;

Encodes a Year (1–9999), Month (1–12) and Day (1–31) into a TDateTime value, the inverse of DecodeDate; raises EConvertError if any parameter is out of its valid range.

EncodeTime

function EncodeTime(Hour: Word; Minute: Word; Second: Word; MilliSecond: Word): TDateTime;

Encodes an Hour (0–23), Minute (0–59), Second (0–59) and MilliSecond (0–999) into a TDateTime value, the inverse of DecodeTime; raises EConvertError if any parameter is out of its valid range.

FormatDateTime

function FormatDateTime(const FormatStr: string; DateTime: TDateTime; Options: TFormatDateTimeOptions = []): string;
function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings; Options: TFormatDateTimeOptions = []): string;

Formats DateTime according to the formatting characters in FormatStr (an empty FormatStr defaults to 'c') and returns the resulting string; raises EConvertError on an invalid formatting character.

IncMonth

function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1): TDateTime;

Adds NumberOfMonths (which may be negative or larger than 12 in absolute value) to the month of DateTime, wrapping the month into the 1–12 range and adjusting the year accordingly.

IsLeapYear

function IsLeapYear(Year: Word): Boolean;

Returns True if Year is a leap year, False otherwise.

MSecsToTimeStamp

function MSecsToTimeStamp(MSecs: Comp): TTimeStamp;

Converts a number of milliseconds (since 1/1/0001) into a TTimeStamp record; use TTimeStamp variables when very precise time tracking is required.

Now

function Now: TDateTime;

Returns the current date and time, equivalent to Date + Time.

StrToDateTime

function StrToDateTime(const S: AnsiString): TDateTime;
function StrToDateTime(const s: ShortString; const FormatSettings: TFormatSettings): TDateTime;
function StrToDateTime(const s: AnsiString; const FormatSettings: TFormatSettings): TDateTime;

Converts a string S, with the date and time parts separated by a space, to a TDateTime value; the date part follows the same rules as StrToDate, with digit order determined by ShortDateFormat, and raises EConvertError on an invalid date, time, or character.

StrToDate

function StrToDate(const S: ShortString): TDateTime;
function StrToDate(const S: Ansistring): TDateTime;
function StrToDate(const S: ShortString; separator: Char): TDateTime;
function StrToDate(const S: AnsiString; separator: Char): TDateTime;
function StrToDate(const S: string; FormatSettings: TFormatSettings): TDateTime;
function StrToDate(const S: ShortString; const useformat: string; separator: Char = #0): TDateTime;
function StrToDate(const S: AnsiString; const useformat: string; separator: Char = #0): TDateTime;
function StrToDate(const S: PChar; Len: Integer; const useformat: string; separator: Char = #0): TDateTime;

Converts a string S, consisting of one to three numbers separated by the DateSeparator character (or an explicitly given separator/format), to a TDateTime date value; if fewer than three numbers are given they are interpreted as day/month or day of the current period, and the digit order (y/m/d, m/d/y, d/m/y) follows ShortDateFormat unless useformat is supplied; raises EConvertError on an invalid date or character.

StrToTime

function StrToTime(const S: Shortstring): TDateTime;
function StrToTime(const S: Ansistring): TDateTime;
function StrToTime(const S: ShortString; separator: Char): TDateTime;
function StrToTime(const S: AnsiString; separator: Char): TDateTime;
function StrToTime(const S: string; FormatSettings: TFormatSettings): TDateTime;
function StrToTime(const S: PChar; Len: Integer; separator: Char = #0): TDateTime;

Converts a string S, consisting of one to four numbers separated by the TimeSeparator character (or an explicitly given separator), to a TDateTime time value; two numbers are interpreted as hour and minutes; raises EConvertError on an invalid time or character.

SystemTimeToDateTime

function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;

Converts a TSystemTime record to a TDateTime value; the inverse operation of DateTimeToSystemTime.

TimeStampToDateTime

function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;

Converts a TTimeStamp record to a TDateTime value; the inverse operation of DateTimeToTimeStamp.

TimeStampToMSecs

function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;

Converts a TTimeStamp value to the number of milliseconds elapsed since 1/1/0001; use TTimeStamp variables when very precise time tracking is required.

TimeToStr

function TimeToStr(Time: TDateTime): string;
function TimeToStr(Time: TDateTime; const FormatSettings: TFormatSettings): string;

Converts Time to a string using the LongTimeFormat setting, equivalent to FormatDateTime('tt', Time).

Time

function Time: TDateTime;

Returns the current time in TDateTime format, with the date portion set to zero.


Other routines in SysUtils: File Name Handling Routines, File Input/Output Routines, Date/Time Routines, Conversion Routines, String Functions, Formatting Strings, PChar Related Functions.