|
огромные возможности форматирования даты в строку
|| 1 procedure DateTimeToString ( var Result : string; const
Formatting : string; DateTime : TDateTime ) ;
|| 2 procedure
DateTimeToString ( var Result : string; const Formatting : string; DateTime :
TDateTime; const FormatSettings : TFormatSettings ) ;
Описание:
Delphi процедура DateTimeToString
позволяет форматировать дату и время, и в нужном формате даты и времени DateTime
( тип TDateTime ) переводить в строку. Т.е Delphi процедура DateTimeToString
позволяет форматировать и время и дату.
Форматирование строки может включать смешивание
обычных символов ( дата будет переведена в строку именно так, как укажет
пользователь). Пример форматирования хорошо представлен в "Пример кода".
Следующее (не-Азиатское) символы формата
даты могут быть использованы:
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
Значения использующиеся
по умолчанию.
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 ...
TwoDigitCenturyWindow = 50
Пример кода:
Формат даты:
var
myDate : TDateTime;
formattedDateTime : string;
begin
//
Set up our TDateTime variable with a full date and time :
//
09/02/2000 at 01:02:03.004 (.004 milli-seconds)
myDate :=
EncodeDateTime(2000, 2, 9, 1, 2, 3, 4);
// Date only -
numeric values with no leading zeroes (except year)
DateTimeToString(formattedDateTime, 'd/m/y', myDate);
ShowMessage(' d/m/y = '+formattedDateTime);
// Date only -
numeric values with leading zeroes
DateTimeToString(formattedDateTime, 'dd/mm/yy', myDate);
ShowMessage(' dd/mm/yy = '+formattedDateTime);
// Use short
names for the day, month, and add freeform text ('of')
DateTimeToString(formattedDateTime, 'ddd d of mmm yyyy', myDate);
ShowMessage(' ddd d of mmm yyyy = '+formattedDateTime);
// Use long names for the day and month
DateTimeToString(formattedDateTime, 'dddd d of mmmm yyyy', myDate);
ShowMessage('dddd d of mmmm yyyy = '+formattedDateTime);
// Use the ShortDateFormat settings only
DateTimeToString(formattedDateTime, 'ddddd', myDate);
ShowMessage(' ddddd = '+formattedDateTime);
// Use the
LongDateFormat settings only
DateTimeToString(formattedDateTime, 'dddddd', myDate);
ShowMessage(' dddddd = '+formattedDateTime);
// Use the
ShortDateFormat + LongTimeFormat settings
DateTimeToString(formattedDateTime, 'c', myDate);
ShowMessage('
c = '+formattedDateTime);
end;
Результат выполнения:
d/m/y =
9/2/00
dd/mm/yy = 09/02/00
ddd d of mmm yyyy = Wed 9
of Feb 2000
dddd d of mmmm yyyy = Wednesday 9 of February 2000
ddddd = 09/02/2000
dddddd = 09 February
2000
c
= 09/02/2000 01:02:03
Пример кода:
Формат времени:
var
myDate : TDateTime;
formattedDateTime : string;
begin
// Set up our
TDateTime variable with a full date and time :
// 09/02/2000 at
01:02:03.004 (.004 milli-seconds)
myDate :=
EncodeDateTime(2000, 2, 9, 1, 2, 3, 4);
// Time only -
numeric values with no leading zeroes
DateTimeToString(formattedDateTime, 'h:m:s.z', myDate);
ShowMessage(' h:m:s.z = '+formattedDateTime);
// Time only
- numeric values with leading zeroes
DateTimeToString(formattedDateTime, 'hh:mm:ss.zzz', myDate);
ShowMessage('hh:mm:ss.zzz = '+formattedDateTime);
// Use
the ShortTimeFormat settings only
DateTimeToString(formattedDateTime, 't', myDate);
ShowMessage('
t = '+formattedDateTime);
// Use the LongTimeFormat
settings only
DateTimeToString(formattedDateTime, 'tt',
myDate);
ShowMessage(' tt = '+formattedDateTime);
// Use the ShortDateFormat + LongTimeFormat settings
DateTimeToString(formattedDateTime, 'c', myDate);
ShowMessage(' c = '+formattedDateTime);
end;
Результат выполнения:
h:m:s.z =
1:2:3.4
hh:mm:ss.zzz =
01:02:03.004
t =
01:02
tt =
01:02:03
c = 09/02/2000 01:02:03
Пример кода:
Полное форматирование:
var
myDate : TDateTime;
formattedDateTime : string;
begin
//
Set up our TDateTime variable with a full date and time
myDate
:= StrToDateTime('09/02/49 01:02:03.004');
// Demonstrate
default locale settings
// Use the DateSeparator and
TimeSeparator values
DateTimeToString(formattedDateTime,
'dd/mm/yy hh:mm:ss', myDate);
ShowMessage('dd/mm/yy hh:mm:ss =
'+formattedDateTime);
// Use ShortMonthNames
DateTimeToString(formattedDateTime, 'mmm', myDate);
ShowMessage(' mmm = '+formattedDateTime);
// Use LongMonthNames
DateTimeToString(formattedDateTime,
'mmmm', myDate);
ShowMessage(' mmmm = '+formattedDateTime);
// Use ShortDayNames
DateTimeToString(formattedDateTime, 'ddd', myDate);
ShowMessage(' ddd = '+formattedDateTime);
// Use
LongDayNames
DateTimeToString(formattedDateTime, 'dddd',
myDate);
ShowMessage(' dddd = '+formattedDateTime);
// Use the ShortDateFormat string
DateTimeToString(formattedDateTime, 'ddddd', myDate);
ShowMessage(' ddddd = '+formattedDateTime);
// Use the
LongDateFormat string
DateTimeToString(formattedDateTime,
'dddddd', myDate);
ShowMessage(' dddddd = '+formattedDateTime);
// Use the TimeAmString
DateTimeToString(formattedDateTime, 'hhampm', myDate);
ShowMessage(' hhampm = '+formattedDateTime);
// Use the
ShortTimeFormat string
DateTimeToString(formattedDateTime, 't',
myDate);
ShowMessage(' t = '+formattedDateTime);
// Use the LongTimeFormat string
DateTimeToString(formattedDateTime, 'tt', myDate);
ShowMessage(' tt = '+formattedDateTime);
// Use the TwoDigitCenturyWindow
DateTimeToString(formattedDateTime, 'dd/mm/yyyy', myDate);
ShowMessage(' dd/mm/yyyy = '+formattedDateTime);
ShowMessage('');
// Now change the
defaults
DateSeparator := '-';
TimeSeparator
:= '_';
ShortDateFormat := 'dd/mmm/yy';
LongDateFormat := 'dddd dd of mmmm of yyyy';
TimeAMString :=
'morning';
TimePMString := 'afternoon';
ShortTimeFormat := 'hh:mm:ss';
LongTimeFormat := 'hh : mm : ss
. zzz';
ShortMonthNames[2] := 'FEB';
LongMonthNames[2] := 'FEBRUARY';
ShortDayNames[4] := 'WED';
LongDayNames[4] := 'WEDNESDAY';
TwoDigitYearCenturyWindow := 75;
// Set up our TDateTime
variable with the same value as before
// except that we must
use the new date and time separators
// The
TwoDigitYearCenturyWindow variable only takes effect here
myDate := StrToDateTime('09-02-49 01_02_03.004');
// Use
the DateSeparator and TimeSeparator values
DateTimeToString(formattedDateTime, 'dd/mm/yy hh:mm:ss', myDate);
ShowMessage('dd/mm/yy hh:mm:ss = '+formattedDateTime);
// Use ShortMonthNames
DateTimeToString(formattedDateTime, 'mmm', myDate);
ShowMessage(' mmm = '+formattedDateTime);
// Use
LongMonthNames
DateTimeToString(formattedDateTime, 'mmmm',
myDate);
ShowMessage(' mmmm = '+formattedDateTime);
// Use ShortDayNames
DateTimeToString(formattedDateTime, 'ddd', myDate);
ShowMessage(' ddd = '+formattedDateTime);
// Use
LongDayNames
DateTimeToString(formattedDateTime, 'dddd',
myDate);
ShowMessage(' dddd = '+formattedDateTime);
// Use the ShortDateFormat string
DateTimeToString(formattedDateTime, 'ddddd', myDate);
ShowMessage(' ddddd = '+formattedDateTime);
// Use the
LongDateFormat string
DateTimeToString(formattedDateTime,
'dddddd', myDate);
ShowMessage(' dddddd = '+formattedDateTime);
// Use the TimeAmString
DateTimeToString(formattedDateTime, 'hhampm', myDate);
ShowMessage(' hhampm = '+formattedDateTime);
// Use the
ShortTimeFormat string
DateTimeToString(formattedDateTime, 't',
myDate);
ShowMessage(' t = '+formattedDateTime);
// Use the LongTimeFormat string
DateTimeToString(formattedDateTime, 'tt', myDate);
ShowMessage(' tt = '+formattedDateTime);
// Use the
TwoDigitCenturyWindow
DateTimeToString(formattedDateTime,
'dd/mm/yyyy', myDate);
ShowMessage(' dd/mm/yyyy =
'+formattedDateTime);
end;
Результат выполнения:
dd/mm/yy hh:mm:ss = 09/02/49 01:02:03
mmm = Feb
mmmm = February
ddd = Tue
dddd = Tuesday
ddddd =
09/02/2049
dddddd
= 09 February
2049
hhampm = 01AM
t =
01:02
tt
=
01:02:03
dd/mm/yyyy
= 09/02/2049
dd/mm/yy hh:mm:ss = 09-02-49
01_02_03
mmm
=
FEB
mmmm = FEBRUARY
ddd = WED
dddd =
WEDNESDAY
ddddd
= 09-FEB-49
dddddd = WEDNESDAY 09 of FEBRUARY of
1949
hhampm
= 01morning
t =
01_02_03
tt
= 01 _ 02 _ 03 . 004
dd/mm/yyyy = 09-02-1949
|
|