[Previous][Up][Next] Reference for unit 'sysutils' (#rtl)

Date and time formatting characters

Various date and time formatting routines accept a format string. to format the date and or time. The following characters can be used to control the date and time formatting:

c
shortdateformat + ' ' + shorttimeformat
d
day of month
dd
day of month (leading zero)
ddd
day of week (abbreviation)
dddd
day of week (full)
ddddd
shortdateformat
dddddd
longdateformat
m
month
mm
month (leading zero)
mmm
month (abbreviation)
mmmm
month (full)
y
year (2 digits)
yy
year (two digits)
yyyy
year (with century)
h
hour
hh
hour (leading zero)
n
minute
nn
minute (leading zero)
s
second
ss
second (leading zero)
t
shorttimeformat
tt
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 seperator
:
insert time seperator
"xx"
literal text
'xx'
literal text

Error in Date/Time Format String

y is supposed to display "year (four digits)", unfortunately, the code
(found in dati.inc) for 'Y' is:

'Y': begin
      if Count>2 then
        StoreInt(Year, 4)
      else
        StoreInt(Year mod 100, 2);
    end;

Which means that a single 'y' only gets two digits, and is therefore
equivalent to "yy".  This can be seen with the output of the following:
----------------------------------------------------------------------
program test_05;

uses SysUtils;

type
  formatOpt = array [ 1..2 ] of string;

const
  formats : array [ 1..24 ] of formatOpt = (
              ( 'c'      , 'shortdateformat + " " + shorttimeformat' ),
              ( 'd'      , 'day of month' ),
              ( 'dd'    , 'day of month (leading zero)' ),
              ( 'ddd'    , 'day of week (abbreviation)' ),
              ( 'dddd'  , 'day of week (full)' ),
              ( 'ddddd'  , 'shortdateformat' ),
              ( 'dddddd' , 'longdateformat' ),
              ( 'm'      , 'month' ),
              ( 'mm'    , 'month (leading zero)' ),
              ( 'mmm'    , 'month (abbreviation)' ),
              ( 'mmmm'  , 'month (full)' ),
              ( 'y'      , 'year (four digits)' ),
              ( 'yy'    , 'year (two digits)' ),
              ( 'yyyy'  , 'year (with century)' ),
              ( 'h'      , 'hour' ),
              ( 'hh'    , 'hour (leading zero)' ),
              ( 'n'      , 'minute' ),
              ( 'nn'    , 'minute (leading zero)' ),
              ( 'a/p'    , 'a/p' ),
              ( 'am/pm'  , 'a/p' ),
              ( 's'      , 'second' ),
              ( 'ss'    , 'second (leading zero)' ),
              ( 't'      , 'shorttimeformat' ),
              ( 'tt'    , 'longtimeformat' )
            );

var
  I  : word;
  desc,
  fs  : String;

begin
  writeln( 'Format | Description                              | Example' );
  writeln( '-------+------------------------------------------+-------------------' );
  for I := 1 to 24 do
    begin
      fs := formats[ I, 1 ];
      desc := formats[ I, 2 ];
      writeln( fs  , '' : (  6 - Length( fs ) ), ' | ',
              desc, '' : ( 40 - Length( desc ) ), ' | ',
              FormatDateTime( fs, Now ) );
    end;
end.
----------------------------------------------------------------------
Format | Description                              | Example
-------+------------------------------------------+-------------------
c      | shortdateformat + " " + shorttimeformat  | 12/19/2006 18:47
d      | day of month                            | 19
dd    | day of month (leading zero)              | 19
ddd    | day of week (abbreviation)              | Tue
dddd  | day of week (full)                      | Tuesday
ddddd  | shortdateformat                          | 12/19/2006
dddddd | longdateformat                          | Tuesday, December 19, 2006
m      | month                                    | 12
mm    | month (leading zero)                    | 12
mmm    | month (abbreviation)                    | Dec
mmmm  | month (full)                            | December
y      | year (four digits)                      | 06          <<< Wrong <<<
yy    | year (two digits)                        | 06
yyyy  | year (with century)                      | 2006
h      | hour                                    | 18
hh    | hour (leading zero)                      | 18
n      | minute                                  | 47
nn    | minute (leading zero)                    | 47
a/p    | a/p                                      | p
am/pm  | a/p                                      | pm
s      | second                                  | 17
ss    | second (leading zero)                    | 17
t      | shorttimeformat                          | 18:47
tt    | longtimeformat                          | 18:47:17
----------------------------------------------------------------------

-- Bob Gibson on December 20, 2006 12:57 AM (view details)