UNIT
DateUtil
Const
FourDigitYear
Type
TDateOrder
TDayOfWeekName
TDaysOfWeek
Routine
CutTime
DateDiff
DaysBetween
DaysInPeriod
DaysPerMonth
DefDateFormat
DefDateMask
IncDate
IncDay
IncHour
IncMinute
IncMonth
IncMSec
IncTime
IncYear
IsLeapYear
LastDayOfPrevMonth
MonthsBetween
StrToDateDef
StrToDateFmt
StrToDateFmtDef
ValidDate
Const
FourDigitYear
Declaration:
const FourDigitYear: Boolean = True;;
To control 2- or 4-digit year in TDateEdit, TDBDateEdit components you can use a typed constant FourDigitYear. The code in initialization section of DateUtil.pas is setting this constant to True or False depending on ShortDateFormat variable. You can set FourDigitYear to True in your project code or in initialization section of your unit (before any of TDateEdit components created) to ensure that your users will be able to enter the year that they need.
When FourDigitYear is False the date entered is always treated as date in the current century so 01 entered now is 1901 but 01 entered in 2000 is 2001.
Type
TDateOrder
Declaration:
TDateOrder = (doMDY, doDMY, doYMD);
An enumerated type used to represent a possible day's, month's and year's order in the date format.
Type
TDayOfWeekName
Declaration:
TDayOfWeekName = (Sun, Mon, Tue, Wed, Thu, Fri, Sat);
An enumerated type used when representing a day of the week.
Type
TDaysOfWeek
Declaration:
TDaysOfWeek = set of TDayNameOfWeek;
Set of day of the week.
Routine
CutTime
Declaration:
function CutTime(ADate: TDateTime): TDateTime;
Set time in the ADate parameter to 00:00:00:00.
CutTime example:
DateWithoutTime := CutTime(ADate);
Routine
DateDiff
Declaration:
procedure DateDiff(Date1, Date2: TDateTime; var Days, Months,
Years: Word);
Return the difference in days, months, and years between two
valid dates Date1 and Date2.
DateDiff example:
var
D1, D2: TDateTime;
D, M, Y: Word;
begin
__ShortDateFormat := 'dd.mm.yyyy';
__D1 := StrToDate('24.03.1994');
__D2 := StrToDate('12.08.1995');
__DateDiff(D1, D2, D, M, Y);
__{ here D=19; M=4; Y=1 }
end;
Routine
DaysBetween
Declaration:
function DaysBetween(Date1, Date2: TDateTime): Longint;
Count days between Date1 and Date2
+ 1.
If Date1 = Date2 then result = 1, if Date2 < Date1 result = 0.
DaysBetween example:
Days := DaysBetween(Date1, Date2);
Routine
DaysInPeriod
Declaration:
function DaysInPeriod(Date1, Date2: TDateTime): Longint;
Count days between Date1 and Date2 + 1, so if Date1 = Date2 result = 1.
> DaysInPeriod example:
Days := DaysInPeriod(Date1, Date2);
Routine
DaysPerMonth
Declaration:
function DaysPerMonth(AYear, AMonth: Integer): Integer;
DaysPerMonth returns the number of days in the specified month AMonth (1..12) and year AYear.
DaysPerMonth example:
The following example returns 28, since 1995 is not a leap year.
var D: Word;
begin
__D := DaysPerMonth(1995, 2);
end;
Routine
DefDateFormat
Declaration:
function DefDateFormat: string;
DefDateFormat returns the default date format string according to the current Windows settings (ShortDateFormat) and to the FourDigitYear variable's value. The order for month, day, and year is determined by the ShortDateFormat global variable.
For example, if the ShortDateFormat = 'D/mm/yy', then the DefDateFormat returns: 'DD/MM/YYYY'.
> DefDateFormat example:
Field1.AsDateTime := StrToDateFmt(DefDateFormat, Edit1.Text);
Routine
DefDateMask
Declaration:
function DefDateMask(BlanksChar: Char): string;
DefDateMask returns the default edit mask for the date (for using as EditMask property value in the TMaskEdit component) according to the current Windows settings (ShortDateFormat) and to the FourDigitYear variable's value. The order for month, day, and year is determined by the ShortDateFormat global variable.
For example, if the ShortDateFormat = 'D/mm/yy', then the DefDateMask returns: '!99/99/9999;1; '.
> DefDateMask example:
MaskEdit1.EditMask := DefDateMask('_');
Routine FirstDayOfNextMonth
Declaration:
function FirstDayOfNextMonth: TDateTime;
Returns the first day of the next month relative to the current system date.
> FirstDayOfNextMonth example:
First := FirstDayOfNextMonth;
Routine FirstDayOfPrevMonth
Declaration:
function FirstDayOfPrevMonth: TDateTime;
Returns the first day of the previous month relative to the current system date.
> FirstDayOfPrevMonth example:
First := FirstDayOfPrevMonth;
Routine
GetDateOrder
Declaration:
function GetDateOrder(const DateFormat: string): TDateOrder;
Returns the day's, month's and year's order in the date format, indicated by the DateFormat parameter.
> GetDateOrder example:
case GetDateOrder(ShortDateFormat)
of
doMDY: Result := 'MM/DD/YYYY';
doDMY: Result := 'DD/MM/YYYY';
doYMD: Result := 'YYYY/MM/DD';
end;
Routine
IncDate
Declaration:
function IncDate(ADate: TDateTime; Days, Months, Years: Integer):
TDateTime;
IncDate adjusts a date by the specified number of days, months, and years. IncDate adds (or subtracts) the specified number of Days, Months, and Years to (or from) a date ADate.
> IncDate example:
var D1, D2: TDateTime; S: string;
begin
ShortDateFormat := 'dd.mm.yy';
D1 := StrToDate('24.03.1994');
D2 := IncDate(D1, 38, 5, 2);
S := DateToStr(D2);
{ here S = '01.10.96' }
end;
Routine
IncDay
Declaration:
function IncDay(ADate: TDateTime; Delta: Integer): TDateTime;
IncDay changes the date ADate by Delta number of days. Delta can be either a positive or negative value.
> IncDay example:
var D1, D2: TDateTime;
S: string;
begin
ShortDateFormat := 'dd.mm.yy';
D1 := StrToDate('24.03.1994');
D2 := IncDay(D1, 145);
S := DateToStr(D2);
end;
Routine
IncHour
Declaration:
function IncHour(ATime: TDateTime; Delta: Integer): TDateTime;
IncHour changes the time of day ATime by Delta number of hours. Delta can be either a positive or negative value.
> IncHour example:
var T1, T2: TDateTime; S: string;
begin
ShortTimeFormat := 'HH:MM:SS';
T1 := StrToTime('00:00:00');
T2 := IncHour(T1, -1);
S := TimeToStr(T2);
end;
Routine
IncMinute
Declaration:
function IncMinute(ATime: TDateTime; Delta: Integer): TDateTime;
IncMinute changes the time of day ATime by Delta number of minutes. Delta can be either a positive or negative value.
> IncMinute example:
var T1, T2: TDateTime; S: string;
begin
ShortTimeFormat := 'HH:MM:SS';
T1 := StrToTime('00:00:00');
T2 := IncMinute(T1, 5);
S := TimeToStr(T2);
end;
Routine
IncMonth
Declaration:
function IncMonth(ADate: TDateTime; Delta: Integer): TDateTime;
IncMonth changes the date ADate by Delta number of months. Delta can be either a positive or negative value.
> IncMonth example:
var D1, D2: TDateTime; S: string;
begin
ShortDateFormat := 'dd.mm.yy';
D1 := StrToDate('30.12.1991');
D2 := IncMonth(D1, 2);
S := DateToStr(D2);
end;
Routine
IncMSec
Declaration:
function IncMSec(ATime: TDateTime; Delta: Integer): TDateTime;
IncMSec changes the time of day ATime by Delta number of milliseconds. Delta can be either a positive or negative value.
> IncMSec example:
var T: TDateTime; Hour, Min, Sec,
MSec;
begin
T := IncMSec(0, -100);
DecodeTime(T, Hour, Min, Sec, MSec);
{ here Hour = 23, Min = 59, Sec = 59, MSec = 900 }
end;
Routine
IncSecond
Declaration:
function IncSecond(ATime: TDateTime; Delta: Integer): TDateTime;
IncSecond changes the time of day ATime by Delta number of seconds. Delta can be either a positive or negative value.
> IncSecond example:
var T1, T2: TDateTime;
S: string;
begin
ShortTimeFormat := 'HH:MM:SS';
T1 := StrToTime('00:00:00');
T2 := IncSecond(T1, -10);
S := TimeToStr(T2);
end;
Routine
IncTime
Declaration:
function IncTime(ATime: TDateTime; Hours, Minutes, Seconds,
MSecs: Integer): TDateTime;
IncTime adds the specified hours, minutes, seconds and milliseconds to the specified time of day ATime. The result of IncTime is adjusted to account for the rollover at midnight.
> IncTime example:
var T1, T2: TDateTime; S: string;
begin
__ShortTimeFormat := 'HH:MM:SS';
__T1 := StrToTime('00:00:00');
__T2 := IncTime(T1, -1, 5, -10, 0);
__S := TimeToStr(T2);
end;
Routine
IncYear
Declaration:
function IncYear(ADate: TDateTime; Delta: Integer): TDateTime;
IncYear changes the date ADate by Delta number of years. Delta can be either a positive or negative value.
> IncYear example:
var D1, D2: TDateTime;
S: string;
begin
__ShortDateFormat := 'dd.mm.yy';
__D1 := StrToDate('30.12.1991');
__D2 := IncYear(D1, -34);
__S := DateToStr(D2);
end;
Routine
IsLeapYear
Declaration:
function IsLeapYear(AYear: Integer): Boolean;
IsLeapYear returns True if the specified year AYear is a leap year.
> IsLeapYear example:
var
__Leap: Boolean;
begin
__Leap := IsLeapYear(1995);
__{ here Leap = False }
end;
Routine LastDayOfPrevMonth
Declaration:
function LastDayOfPrevMonth: TDateTime;
Returns the last day of the previous month relative to the current system date.
> LastDayOfPrevMonth example:
Last := LastDayOfPrevMonth;
Routine
MonthsBetween
Declaration:
function MonthsBetween(Date1, Date2: TDateTime): Double;
Count months between Date1 and Date2.
> MonthsBetween example:
Res := MonthsBetween(Date1, Date2);
Routine
StrToDateDef
Declaration:
function StrToDateDef(const S: string; Default: TDateTime):
TDateTime;
StrToDateDef converts a string to a date format. The order for month, day, and year is determined by the ShortDateFormat global variable. If the given string does not contain a valid date, StrToDateDef returns the date passed in Default.
> StrToDateDef example:
Field1.AsDateTime := StrToDateDef(Edit1.Text, SysUtils.Date);
Routine
StrToDateFmt
Declaration:
function StrToDateFmt(const DateFormat, S: string): TDateTime;
StrToDateFmt converts a string to
a date format. The order for month, day, and year is determined
by the DateFormat parameter. If the given string does not contain
a valid date, an EConvertError exception is raised.
> StrToDateFmt example:
Field1.AsDateTime := StrToDateFmt(DefDateFormat, Edit1.Text);
Routine
StrToDateFmtDef
Declaration:
function StrToDateFmtDef(const DateFormat, S: string; Default:
TDateTime): TDateTime;
StrToDateFmtDef converts a string to a date format. The order for month, day, and year is determined by the DateFormat parameter.
If the given string does not contain a valid date, StrToDateFmtDef returns the date passed in Default.
> StrToDateFmtDef example:
Field1.AsDateTime := StrToDateFmtDef(DefDateFormat, Edit1.Text, SysUtils.Date);
Routine
ValidDate
Declaration:
function ValidDate(ADate: TDateTime): Boolean;
ValidDate verifies that the specified date ADate is a valid date.
ValidDate example:
if ValidDate(ADate) then
begin
__{ deal with ADate }
end;
Index Page | About | Download
Creation Date: 4 Feb 1998 | Last Update: 16 Mar 2000