You are on page 1of 1

Did You Know?

Delphi’s Date/
Time Functions
by Cary Jensen
This information and much more is available on Cary’s bi-monthly, free Developer Days ELetter, to which you can sign
up on http://www.DeveloperDays.com .

Beginning with Delphi 6, Delphi’s runtime library (RTL) IsToday Pass in a TDateTime. Returns a Boolean value
was expanded with a number of new units. One of these, indicating whether or not that TDateTime represents
DateUtils.pas, contains more than 150 the same date as the current date.
date/time-related routines and associated constants. If your
MinuteOfTheDay Pass a TDateTime parameter. Returns
applications ever need to work with dates
the number of minutes since midnight that this
TDateTime values, this unit deserves your close inspection.
TDateTime represents.
Here are just of few of the interesting routines that you can
SameDate Pass in two TDateTime parameters. Returns
find in this unit:
a Boolean value indicating whether or not those two
CompareDate Requires two TDateTime values. Returns TDateTimes represent the same date.
whether the first date is prior to, is equal to, or is
StartOfAMonth Pass in a year and month number.
later than the second date.
Returns a TDateTime value that points to the first
DaysInMonth Pass a TDateTime value. Returns the day of that month.
number of days in the month associated with the
StartOfAWeek Similar to StartOfAMonth, returns the
passed date.
first day of the week associated with the given date.
DayOfTheMonth Pass a TDateTime parameter. Returns This function is ISO 8601 compliant. More about
the number of days represented by this TDateTime this is said later in his section.
since the last day of the preceding month.
WithinPastHours Pass two TDateTime values and
DaySpan Pass two TDateTime values. Returns the some number of hours. Returns a Boolean value
number of days between the two dates. indicating if the two TDateTimes represent values
within the specified number of hours.
DecodeDayMonthWeek Pass a TDateTime parameter
and four Word out parameters. The TDateTime is YearOf Pass in a TDateTime. Returns the year number
decoded, and the four Word parameters are assigned of the specified date.
the year number, month number, week into the year,
This is just a small sampling of the routines that you will
and day of the week, respectively.
find in the DateUtils unit. In addition, nearly every category
EndOfAMonth Pass in a year number and a month of function has its equivalents for each
number, both as Word values. Returns a TDateTime time metric. For example, the following functions appear
value for the last day of the specified month. in the DateUtils unit: YearOf, MonthOf, WeekOf, DayOf,
HourOf, MinuteOf, SecondOf, and MilliSecondOf.
EndOfAYear Pass a year number as a Word. Returns a
TDateTime value for the last day of that year. Many of the functions in the DateUtils unit are ISO 8601
compliant. As documented with comments at the beginning
HourOfTheYear Pass a TDateTime value. Returns the
of the DateUtils unit, ISO 8601 specifies what
number of hours into the year represented by that
constitutes the first week of the year, the first day of a month,
TDateTime.
and so on. ISO 8601compliant functions are flagged using
IncDay Pass in a TDateTime and some number of days. comments in the interface section of the DateUtils unit.For
Returns a TDateTime specified number of days in additional information on ISO 8601, see http://www.iso.ch/
the future. iso/en/ISOOnline.frontpage and search for 8601.
IncMinutes Same as IncDay, but returns a TDateTime Some of the functions and concepts used in the DateUtils
the specified number of minutes in the future. unit where contributed by Delphi developers, including
IsInLeapYear Pass a TDateTime. Returns a Boolean Jeroen Pluimers, Glenn Crouch, Rune Moberg,
value indicating whether or not the year in which the and Ray Lischner. Thanks, guys.
TDateTime resides in a leap year. The DateUtils.pas unit is located in the Source/Rtl/
IsValidDate Pass a year number, month number, and day Common/DateUtils.pas file, located under the directory in
number. Returns a Boolean value indicating whether which you installed Delphi 6 or later.
the numbers constitute a real date.

You might also like