Format Date Time
Transcript of Format Date Time
FormatDateTime Function
Rich formatting of a TDateTime variable into a string SysUtils unit
1 function FormatDateTime ( const Formatting : string; DateTime : TDateTime ) : string;
2 function FormatDateTime ( const Formatting : string; DateTime : TDateTime; const FormatSettings : TFormatSettings ) : string;
Description
The FormatDateTime function provides rich formatting of a TDateTime value DateTime into a string. Formatting is defined by the Formatting string. The Formatting string can comprise a mix of ordinary characters (that are passed unchanged to the result string), and data formatting characters. This formatting is best explained by the example code. The following (non-Asian) 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 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 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
Important : if you want to see characters such as dd in the formatted output, placing them in " marks will stop them being interpreted as date or time elements. In addition to this formatting, various of the above options are affected by the following variables, withe 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
Example code : Showing all of the date field formatting data types
var myDate : TDateTime; begin // Set up our TDateTime variable with a full date and time : // 5th of June 2000 at 01:02:03.004 (.004 milli-seconds) myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4); // Date only - numeric values with no leading zeroes (except year) ShowMessage(' d/m/y = '+ FormatDateTime('d/m/y', myDate)); // Date only - numeric values with leading zeroes ShowMessage(' dd/mm/yy = '+ FormatDateTime('dd/mm/yy', myDate)); // Use short names for the day, month, and add freeform text ('of') ShowMessage(' ddd d of mmm yyyy = '+ FormatDateTime('ddd d of mmm yyyy', myDate)); // Use long names for the day and month ShowMessage('dddd d of mmmm yyyy = '+ FormatDateTime('dddd d of mmmm yyyy', myDate)); // Use the ShortDateFormat settings only ShowMessage(' ddddd = '+ FormatDateTime('ddddd', myDate)); // Use the LongDateFormat settings only ShowMessage(' dddddd = '+ FormatDateTime('dddddd', myDate)); // Use the ShortDateFormat + LongTimeFormat settings ShowMessage(' c = '+ FormatDateTime('c', myDate)); end;
Show full unit code
d/m/y = 5/6/00 dd/mm/yy = 05/06/00 ddd d of mmm yyyy = Mon 5 of Jun 2000 dddd d of mmmm yyyy = Monday 5 of June 2000 ddddd = 05/06/2000 dddddd = 05 June 2000 c = 05/06/2000 01:02:03
Example code : Showing all of the time field formatting data types
var myDate : TDateTime; begin // Set up our TDateTime variable with a full date and time : // 5th of June 2000 at 01:02:03.004 (.004 milli-seconds) myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4); // Time only - numeric values with no leading zeroes ShowMessage(' h:n:s.z = '+FormatDateTime('h:n:s.z', myDate)); // Time only - numeric values with leading zeroes ShowMessage('hh:nn:ss.zzz = '+FormatDateTime('hh:nn:ss.zzz', myDate)); // Use the ShortTimeFormat settings only ShowMessage(' t = '+FormatDateTime('t', myDate)); // Use the LongTimeFormat settings only ShowMessage(' tt = '+FormatDateTime('tt', myDate)); // Use the ShortDateFormat + LongTimeFormat settings ShowMessage(' c = '+FormatDateTime('c', myDate)); end;
VerfurnIt tamak
Re
Dat
Dat
Dat
Lon
Lon
Lon
Lon
Sho
Sho
Sho
Sho
StrT
Tim
Tim
Tim
Two
rsion 2 onish the Fakes a locke the ro
lated co
teSepara
teTimeTo
teTimeTo
ngDateFo
ngDayNa
ngMonth
ngTimeFo
ortDateF
ortDayNa
ortMonth
ortTimeF
ToDateT
meAMStr
mePMStri
meSepara
oDigitYe
Down
of this funFormatSecal copy outine thre
omman
ator
oStr
oString
ormat
ames
Names
ormat
Format
ames
hNames
Format
Time
ing
ing
ator
earCentu
nload this
nction is foettings rof global ead safe.
nds
uryWindo
s web site
or use wirecord befformattin
The csepafields
Convand tstring
Rich TDatstring
Longto str
An arweekSund
An armont= Jan
Longto str
Comdate
An arweekSund
An armont= Jan
Shorto str
Convstringvalue
DeteDateTproce
DeteDateTproce
The csepafields
ow Sets for 2conv
e as a Win
thin threafore invo
ng variabl
characterrate disps verts TDatime valug formattinteTime vag version oring formrray of dak names, day rray of dath namesnuary version oring formpact vers to string rray of dak names, day rray of dath namesn rt version ring formverts a dag into a Te rmines ATimeToStedure rmines PMTimeToStedure characterrate disps the centu digit yeaersions
ndows pr
ads. You king the cles that
r used to lay date
teTime daes to a
ng of a ariable int
of the datmat ays of the starting
ays of thes, starting
of the timmat sion of the format ays of the starting
ays of thes, starting
of the timmat ate+time TDateTim
M value itring
M value itring
r used to lay time
ury thresar string
rogram.
call.
ate
to a
te
e 1 =
e g 1
me
e
e 1 =
e g 1
me
e
n
n
hold
E
v
b
hh:mm
Example
var myDate
begin // Set // 5th // // Not // // // // // Co // // (58
myDate
// Dem
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
// Use ShowMe
ShowMe
// Now DateSe TimeSe ShortD LongDa TimeAM TimePM ShortT LongTi ShortM LongMo ShortD LongDa TwoDig
// Set // exc
h:m:s.m:ss.zz t
e code
e : TDa
t up ouh of Ju
te that Subt ompare So t8 would
e := St
monstra
e the Dessage(
e Shortessage(
e LongMessage(
e Shortessage(
e LongDessage(
e the Sessage(
e the Lessage(
e the Tessage(
e the Sessage(
e the Lessage(
e the Tessage(
essage(
w changeparatoeparatoDateForateFormMStringMStringTimeForimeFormMonthNaonthNamDayNameayNamesgitYear
t up oucept th
.z = 1:zz = 01t = 01tt = 01c = 05
: Show
ateTime
ur TDatune 204
t 49 is T tract T 2 diwith tthe yead be co
trToDat
ate def
DateSep('dd/mmFormat
tMonthN('
MonthNa('
tDayNam('
DayName('
ShortDa('
LongDat('
TimeAmS('
ShortTi('
LongTim('
TwoDigi(' Format
('');
ge the or or rmat mat g g rmat mat ames[6]mes[6]es[1] s[1] rCentur
ur TDathat we
2:3.4 1:02:03.1:02 1:02:035/06/200
wing the
e;
teTime v49 at 01
s treateTwoDigit TwoDigitigit yeathe lastar is inonverted
teTime('
fault lo
parator m/yy hh:tDateTim
Names
ames
mes
es
ateForma
teFormat d
String h
imeForma
meFormat
itCentur dd/mmtDateTim
default := '-' := '_' := 'dd := 'dd := 'mo := 'af := 'hh := 'hh := 'JU := 'JU := 'SU := 'SUryWindow
teTime vmust us
.004
00 01:0
e effect
variabl1:02:03
ed as 2tYearCe CtYearCear to bt 2 dign the nd to 19
'05/06/
ocale s
and Ti:nn:ss me('dd/
mmm
mmmm
ddd
dddd
at striddddd
t strindddddd
hhampm
at stri t
t strin tt
ryWindom/yyyy me('dd/
ts '; '; d/mmm/yddd dd orning'fternooh:nn:ssh : nn UN'; UNE';UN'; UNDAY';w := 75
variablse the
02:03
of loca
le with3.004
2049 asenturyWCurrententuryWbe convgits ofnext ce958)
/49 01:
setting
imeSepa= '+ /mm/yy
= '+Fo
= '+Fo
= '+Fo
= '+Fo
ing = '+Fo
ng = '+Fo
= '+Fo
ing = '+Fo
ng = '+Fo
ow = '+ /mm/yyy
yy'; of mmm'; on'; s'; : ss .
; 5; // T
le withnew da
al date f
h a full(.004 m
s followWindow =t year =Window =verted =f 1958 =entury =
02:03.0
gs
arator v
hh:nn:s
ormatDat
ormatDat
ormatDat
ormatDat
ormatDat
ormatDat
ormatDat
ormatDat
ormatDat
yy', myD
mm of yy
zzz';
This mea
h the saate and
format
l date milli-s
ws : => 50=> 2008=> 1958=> 49=> Less=> 2049
004');
values
ss', my
teTime(
teTime(
teTime(
teTime(
teTime(
teTime(
teTime(
teTime(
teTime(
Date));
yyy';
ans 49
ame valtime s
setting
and tiseconds
8 (at t8
s 9
yDate))
('mmm',
('mmmm'
('ddd',
('dddd'
('ddddd
('ddddd
('hhamp
('t', m
('tt',
;
is tre
lue as separat
gs
ime : s)
time of
;
myDate
, myDat
myDate
, myDat
d', myDa
dd', myD
pm', myD
myDate))
myDate)
eated as
beforetors
Show full u
writin
e));
te));
e));
te));
ate));
Date));
Date));
);
));
s 1949
unit code
ng)
;
;
// The TwoDigitYearCenturyWindow variable only takes effect here myDate := StrToDateTime('09-02-49 01_02_03.004'); // Use the DateSeparator and TimeSeparator values ShowMessage('dd/mm/yy hh:nn:ss = '+ FormatDateTime('dd/mm/yy hh:nn:ss', myDate)); // Use ShortMonthNames ShowMessage(' mmm = '+FormatDateTime('mmm', myDate)); // Use LongMonthNames ShowMessage(' mmmm = '+FormatDateTime('mmmm', myDate)); // Use ShortDayNames ShowMessage(' ddd = '+FormatDateTime('ddd', myDate)); // Use LongDayNames ShowMessage(' dddd = '+FormatDateTime('dddd', myDate)); // Use the ShortDateFormat string ShowMessage(' ddddd = '+FormatDateTime('ddddd', myDate)); // Use the LongDateFormat string ShowMessage(' dddddd = '+FormatDateTime('dddddd', myDate)); // Use the TimeAmString ShowMessage(' hhampm = '+FormatDateTime('hhampm', myDate)); // Use the ShortTimeFormat string ShowMessage(' t = '+FormatDateTime('t', myDate)); // Use the LongTimeFormat string ShowMessage(' tt = '+FormatDateTime('tt', myDate)); // Use the TwoDigitCenturyWindow ShowMessage(' dd/mm/yyyy = '+ FormatDateTime('dd/mm/yyyy', myDate)); end;
Show full unit code
dd/mm/yy hh:mm:ss = 05/06/49 01:02:03 mmm = Jun mmmm = June ddd = Sat dddd = Saturday ddddd = 05/06/2049 dddddd = 05 June 2049 hhampm = 01AM t = 01:02 tt = 01:02:03 dd/mm/yyyy = 05/06/2049 dd/mm/yy hh:nn:ss = 05-06-49 01_02_03 mmm = JUN mmmm = JUNE ddd = SUN dddd = SUNDAY ddddd = 05-JUN-49 dddddd = SUNDAY 05 of JUNE of 1949 hhampm = 01morning t = 01_02_03 tt = 01 _ 02 _ 03 . 004 dd/mm/yyyy = 05-06-1949