uses SysUtils and DateUtils
(一)
var
date1, date2, date3 : TDateTime;
begin
date1 := Yesterday;
date2 := Date;
date3 := Tomorrow;
date4 := Now;
end;
date1 會是 12/02/2012 00:00:00
date2 會是 13/02/2012 00:00:00
date3 會是 14/02/2012 00:00:00
date4 會是 13/02/2012 08:15:45
(二) 長短月份(month)名稱
var
month : Integer;
begin
for month := 1 to 12 do
begin
ShowMessage(ShortMonthNames[month]);
ShowMessage(LongMonthNames[month]);
end;
end;
結果顯示
Jan
January
Feb
February
Mar
March
Apr
April
May
May
Jun
June
Jul
July
Aug
August
Sep
September
Oct
October
Nov
November
Dec
December
(三) 長短周日(day)名稱
var
day : Integer;
begin
for day := 1 to 7 do
begin
ShowMessage(ShortDayNames[day]);
ShowMessage(LongDayNames[day]);
end;
end;
結果顯示
Sun
Sunday
Mon
Monday
Tue
Tuesday
Wed
Wednesday
Thu
Thursday
Fri
Friday
Sat
Saturday
(四) 日期計算
DayOfTheMonth Gives the day of month index for a TDateTime value
DaysBetween Gives the whole number of days between 2 dates
DaysInAMonth Gives the number of days in a month
DaysInAYear Gives the number of days in a year DayOfTheYear Gives the number of days from Jan 1 of the year
eg: exactdays := IntToStr(DayOfTheYear(MonthCalendar1.Date));
DecodeDate Extracts the year, month, day values from a TDateTime var.
EncodeDate Build a TDateTime value from year, month and day values
IncMillisecond
Increments a TDateTime variable by + or - number of milliseconds
IncSecond
Increments a TDateTime variable by + or - number of seconds
IncMinute
Increments a TDateTime variable by + or - number of minutes
IncDay Increments a TDateTime variable by + or - number of days
eg: Convert threee digits of dayoftheYear back to DataTime Format
date1: TDateTime;
days1 : integer;
date1 := IncDay( EncodeDate(2000+year1,1,1), days1-1);
MonthCalendar.Date := date1;
IncMonth
Increments a TDateTime variable by a number of months
IncYear
Increments a TDateTime variable by a number of years
IsLeapYear Returns true if a given calendar year is a leap year
MinsPerDay Gives the number of minutes in a day
例子
Mydate, Mytime: TDateTime;
strDate, strTime: String;
Mydate:= EncodeDate(2012,4,5); //year, month, day
strDate := dateToStr(Mydate);
Mytime:= EncodeTime(15,33,35,3); //hour, min, sec, msec
strTime := TimeToStr(Mytime);
還有 dateTimeToStr
(五) 顯示date 及 time 值的不同格式
var
myDate : TDateTime;
begin
// Set up our TDateTime variable with a full date and time :
// 09/02/2000 at 05:06:07.008 (.008 milli-seconds)
myDate := EncodeDateTime(2000, 2, 9, 5, 6, 7, 8);
// 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));
ShowMessage('');
// 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;
The ShowMessage routine shows the following outputs :
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
h:n:s.z = 1:2:3.4
hh:nn:ss.zzz = 01:02:03.004
t = 01:02
tt = 01:02:03
c = 09/02/2000 01:02:03
The above output uses default values of a number of formatting control variables. These are covered
in the next section:
Formatting control variables
The variables and their default values are given below. Note that these control conversions of date
time values to strings, and sometimes from strings to date time values (such as DateSeparator).
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
|