0% found this document useful (0 votes)
187 views3 pages

Format Date Time

The FormatDateTime function formats a TDateTime value as a string using a specified format. It supports formatting dates, times, or datetimes with options like numeric values with or without leading zeros, short and long month/day names, and 12 or 24-hour time. The formatting is affected by variables like DateSeparator, TimeSeparator, and short/long date/time formats. Version 2 supports threading by making a local copy of global formatting variables.

Uploaded by

Rui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
187 views3 pages

Format Date Time

The FormatDateTime function formats a TDateTime value as a string using a specified format. It supports formatting dates, times, or datetimes with options like numeric values with or without leading zeros, short and long month/day names, and 12 or 24-hour time. The formatting is affected by variables like DateSeparator, TimeSeparator, and short/long date/time formats. Version 2 supports threading by making a local copy of global formatting variables.

Uploaded by

Rui
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FormatDateTime Rich formatting of a TDateTime variable into a string SysUtils unit

Function

1 function FormatDateTime ( const Formatting : string; DateTime : TDateTime ) : string;


2 function FormatDateTime ( const Formatting : string; DateTime : TDateTime; const FormatSettings : TFormatSettings ) :
string;

Description Example code : Showing all of the date field formatting data types

The FormatDateTime function provides rich formatting of a var


TDateTime value DateTime into a string. Formatting is myDate : TDateTime;
defined by the Formatting string.
begin
The Formatting string can comprise a mix of ordinary // Set up our TDateTime variable with a full date and time :
characters (that are passed unchanged to the result string), // 5th of June 2000 at 01:02:03.004 (.004 milli-seconds)
and data formatting characters. This formatting is best myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4);
explained by the example code.

The following (non-Asian) formatting character strings can


// Date only - numeric values with no leading zeroes (except year)
be used in the Formatting string: ShowMessage(' d/m/y = '+
FormatDateTime('d/m/y', myDate));
y = Year last 2 digits
// Date only - numeric values with leading zeroes
yy = Year last 2 digits
ShowMessage(' dd/mm/yy = '+
yyyy = Year as 4 digits FormatDateTime('dd/mm/yy', myDate));
m = Month number no-leading 0
mm = Month number as 2 digits // Use short names for the day, month, and add freeform text ('of')
mmm = Month using ShortDayNames (Jan) ShowMessage(' ddd d of mmm yyyy = '+
mmmm = Month using LongDayNames (January)
FormatDateTime('ddd d of mmm yyyy', myDate));
d = Day number no-leading 0
// Use long names for the day and month
dd = Day number as 2 digits ShowMessage('dddd d of mmmm yyyy = '+
ddd = Day using ShortDayNames (Sun) FormatDateTime('dddd d of mmmm yyyy', myDate));
dddd = Day using LongDayNames (Sunday)
ddddd = Day in ShortDateFormat // Use the ShortDateFormat settings only
ShowMessage(' ddddd = '+
dddddd = Day in LongDateFormat
FormatDateTime('ddddd', myDate));

c = Use ShortDateFormat + LongTimeFormat // Use the LongDateFormat settings only


h = Hour number no-leading 0 ShowMessage(' dddddd = '+
hh = Hour number as 2 digits FormatDateTime('dddddd', myDate));
n = Minute number no-leading 0
// Use the ShortDateFormat + LongTimeFormat settings
nn = Minute number as 2 digits
ShowMessage(' c = '+
s = Second number no-leading 0 FormatDateTime('c', myDate));
ss = Second number as 2 digits end;
z = Milli-sec number no-leading 0s
Show full unit code
zzz = Milli-sec number as 3 digits
t = Use ShortTimeFormat d/m/y = 5/6/00
tt = Use LongTimeFormat 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
am/pm = Use after h : gives 12 hours + am/pm
ddddd = 05/06/2000
a/p = Use after h : gives 12 hours + a/p dddddd = 05 June 2000
ampm = As a/p but TimeAMString,TimePMString c = 05/06/2000 01:02:03
/ = Substituted by DateSeparator value
: = Substituted by TimeSeparator value

Important : if you want to see characters such as dd in the


Example code : Showing all of the time field formatting data types
formatted output, placing them in " marks will stop them
being interpreted as date or time elements. var
myDate : TDateTime;
In addition to this formatting, various of the above options
are affected by the following variables, withe their default begin
values : // Set up our TDateTime variable with a full date and time :
// 5th of June 2000 at 01:02:03.004 (.004 milli-seconds)
DateSeparator = / myDate := EncodeDateTime(2000, 6, 5, 1, 2, 3, 4);
TimeSeparator = :
ShortDateFormat = dd/mm/yyyy // Time only - numeric values with no leading zeroes
LongDateFormat = dd mmm yyyy ShowMessage(' h:n:s.z = '+FormatDateTime('h:n:s.z', myDate));
TimeAMString = AM
// Time only - numeric values with leading zeroes
TimePMString = PM
ShowMessage('hh:nn:ss.zzz = '+FormatDateTime('hh:nn:ss.zzz', myDate));
ShortTimeFormat = hh:mm
LongTimeFormat = hh:mm:ss // Use the ShortTimeFormat settings only
ShortMonthNames = Jan Feb ... ShowMessage(' t = '+FormatDateTime('t', myDate));
LongMonthNames = January, February ...
// Use the LongTimeFormat settings only
ShortDayNames = Sun, Mon ...
ShowMessage(' tt = '+FormatDateTime('tt', myDate));
LongDayNames = Sunday, Monday ...
TwoDigitYearCenturyWindow = 50 // Use the ShortDateFormat + LongTimeFormat settings
ShowMessage(' c = '+FormatDateTime('c', myDate));
end;
Verrsion 2 o of this fun
nction is fo
or use within threa
ads. You Show full unit
u code
furnnish the F
FormatSe ettings rrecord beffore invoking the c
call.
It ta
akes a loccal copy oof global formattin
ng variablles that h:m:s.
.z = 1:2:3.4
mak ke the routine thre ead safe. hh:mm
m:ss.zzzz = 1:02:03.
01 .004
t = 1:02
01
t
tt = 1:02:03
01
c = 5/06/200
05 00 01:0
02:03
Related co
omman
nds

DatteSepara
ator The ccharacterr used to
separate display date E
Example wing the
e code : Show e effect of loca
al date format
f setting
gs
fields
s
DatteTimeTooStr Conv verts TDateTime da ate
and ttime values to a v
var
stringg myDate
e : TDa e;
ateTime
DatteTimeTooString Rich formattin ng of a
TDatteTime va ariable intto a b
begin
stringg // Set
t up ou teTime variabl
ur TDat v le with
h a full
l date and ti
ime :
Lon
ngDateFo ormat Long version o of the datte // 5th
h of Ju 49 at 01
une 204 1:02:03
3.004 (.004 milli-s
m seconds
s)
to strring form
mat //
Lon
ngDayNa ames An arrray of daays of thee
// Not
te that s treate
t 49 is ed as 2049
2 as
s follow
ws :
week k names, starting 1 =
Sund day
// TwoDigit
T tYearCe
enturyW
Window =>
= 50
Lon
ngMonthNames An arrray of daays of thee // C
Current
t year =>
= 20088 (at time
t of writin
ng)
montth names s, starting
g1 // tract TwoDigit
Subt T tYearCe
enturyW
Window =>
= 19588
= Jannuary // igit yea
2 di ar to be
b convverted =>
= 49
Lon
ngTimeFo ormat Long version o of the tim
me // Coompare with the
t last
t 2 dig
gits of
f 1958 =>
= Lesss
to strring form
mat // So t ar is in
the yea n the next
n ce
entury =>
= 20499
ShoortDateF
Format Compact vers sion of thee // (58
8 would onverted
d be co d to 19
958)
date to string format
ShoortDayNaames An arrray of daays of thee myDate
e := St teTime('
trToDat '05/06/
/49 01:02:03.0
004');
week k names, starting 1 =
Sund day
// Dem
monstra fault lo
ate def ocale setting
s gs
ShoortMonth
hNames An arrray of daays of thee
montth names s, starting
g1
= Jann // Use
e the D
DateSepparator and Ti
imeSepa
arator values
v
ShoortTimeF
Format Shorrt version of the tim me ShowMe
essage(
('dd/mmm/yy hh:
:nn:ss = '+
to strring form
mat tDateTim
Format me('dd/
/mm/yy hh:nn:s
ss', my
yDate));
StrT
ToDateTTime Conv verts a daate+time
stringg into a T
TDateTime // Use
e Short Names
tMonthN
valuee ShowMe
essage(
(' mmm = '+Fo
ormatDat
teTime(
('mmm', myDate
e));
Tim
meAMString Determines AM value in
DateT TimeToSttring // Use
e LongM ames
MonthNa
proceedure
ShowMe
essage(
(' mmmm = '+Fo
ormatDat
teTime(
('mmmm', myDat
te));
Tim
mePMStriing Determines PM M value in
DateT TimeToSttring
proceedure // Use
e Short mes
tDayNam
Tim
meSeparaator The ccharacterr used to ShowMe
essage(
(' ddd = '+Fo
ormatDat
teTime(
('ddd', myDate
e));
separate display time
fields
s // Use
e LongD
DayName
es
TwooDigitYe
earCentuuryWindo
ow Sets the centu ury threshold ShowMe
essage(
(' dddd = '+Fo
ormatDat
teTime(
('dddd', myDat
te));
for 2 digit yeaar string
conversions // Use
e the S
ShortDa
ateForma
at stri
ing
ShowMe
essage(
(' ddddd = '+Fo
ormatDat
teTime(
('ddddd
d', myDa
ate));

Down
nload this
s web site
e as a Win
ndows prrogram. // Use
e the L
LongDat
teFormat
t strin
ng
ShowMe
essage(
(' d
dddddd = '+Fo
ormatDat
teTime(
('ddddd
dd', myD
Date));
;

// Use
e the T
TimeAmS
String
ShowMe
essage(
(' h
hhampm = '+Fo
ormatDat
teTime(
('hhamp
pm', myD
Date));
;

// Use
e the S
ShortTi
imeForma
at stri
ing
ShowMe
essage(
(' t = '+Fo
ormatDat
teTime(
('t', myDate))
m );

// Use
e the L
LongTim
meFormat
t strin
ng
ShowMe
essage(
(' tt = '+Fo
ormatDat
teTime(
('tt', myDate)
));

// Use
e the T
TwoDigiitCentur
ryWindo
ow
ShowMe
essage(
(' dd/mm
m/yyyy = '+
Format
tDateTim
me('dd/
/mm/yyy
yy', myD
Date));
;

ShowMe
essage(
('');

// Now
w chang
ge the default
ts
DateSe
eparato
or := '-'
';
TimeSe
eparato
or := '_'
';
ShortD
DateFor
rmat := 'dd
d/mmm/y
yy';
LongDa
ateForm
mat := 'dd
ddd dd of mmm
mm of yy
yyy';
TimeAM
MString
g := 'mo
orning'
';
TimePM
MString
g := 'af
fternoo
on';
ShortT
TimeFor
rmat := 'hh
h:nn:ss
s';
LongTi
imeForm
mat := 'hh
h : nn : ss . zzz';
ShortM
MonthNa
ames[6] := 'JU
UN';
LongMo
onthNam
mes[6] := 'JUUNE';
ShortD
DayName
es[1] := 'SU
UN';
LongDa
ayNames
s[1] := 'SU
UNDAY';
;
TwoDig
gitYear
rCentur
ryWindow
w := 75
5; // This
T mea
ans 49 is tre
eated as
s 1949

// Set
t up ou
ur TDat
teTime variabl
v le with
h the sa
ame val
lue as before
// exc
cept th
hat we must us
se the new da
ate and time separat
s tors
// 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy