FormatDateTime converts a DateTime value into a String data type, using an acceptable provided format.

The FormatDateTime function provides rich formatting of the TDateTime value DateTime into a string.

Formatting is defined by the formatting string Format, which can comprise a mix of ordinary characters (that are passed unchanged to the result string), and data formatting characters - see below for a list of characters.

This formatting is best explained by the example code.

More information can be reviewed at Date And Time Format Specifiers.

This is commonly used when you need to build a String and concatenate a Datetime value.

Declaration: Function FormatDateTime( const Format : string; DateTime : TDateTime) : string

An example,

procedure OnMapEvent(var Value:Variant);
var
MyDate : TDateTime;
begin
MyDate := Now;
//Value will be equal to the time of the call e.g. '26/07/2006 18:35'
Value := FormatDateTime('dd/mm/yyyy hh:nn',MyDate);
end;

This function is also classed as a DateTime Function.

The following formatting character strings can be used in the formatting string -

  • y = Year last 2 digits

  • yy = Year last 2 digits

  • yyyy = Year as 4 digits

  • m = Month number no-leading zero (0)

  • mm = Month number as 2 digits

  • mmm = Month using ShortDayNames (Jan)

  • mmmm = Month using LongDayNames (January)

  • d = Day number no-leading zero (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

If you want to see characters such as dd in the formatted output, placing them in quote marks (") will stop them being interpreted as date or time elements.

  • c = Use ShortDateFormat + LongTimeFormat

  • h = Hour number no-leading zero (0)

  • hh = Hour number as 2 digits

  • n = Minute number no-leading zero (0)

  • nn = Minute number as 2 digits

  • s = Second number no-leading zero (0)

  • ss = Second number as 2 digits

  • z = Millisecond number no-leading zero (0)

  • zzz = Millisecond number as 3 digits

  • t = Use ShortTimeFormat

  • tt = Use LongTimeFormat

  • am/pm = Use after h : gives 12 hours + am/pm

  • a/p = Use after h : gives 12 hours + a/p

  • ampm = As a/p but TimeAMString,TimePMString

  • / = Substituted by DateSeparator value

  • : = Substituted by TimeSeparator value

In addition to this formatting, various of the above options are affected by the following variables, with their default values -

  • DateSeparator = /

  • TimeSeparator = :

  • ShortDateFormat = dd/mm/yyyy

  • LongDateFormat = dd mmm yyyy

  • TimeAMString = AM

  • TimePMString = PM

  • ShortTimeFormat = hh:mm

  • LongTimeFormat = hh:mm:ss

  • ShortMonthNames = Jan Feb ...

  • LongMonthNames = January, February ...

  • ShortDayNames = Sun, Mon ...

  • LongDayNames = Sunday, Monday ...

  • TwoDigitYearCenturyWindow = 50