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

Date Time

Embed Size (px)

Citation preview

Page 1: Date Time

Beginning with Delphi 6, Delphi’s runtime library (RTL)was expanded with a number of new units. One of these,DateUtils.pas, contains more than 150date/time-related routines and associated constants. If yourapplications ever need to work with datesTDateTime values, this unit deserves your close inspection.

Here are just of few of the interesting routines that you canfind in this unit:

CompareDate Requires two TDateTime values. Returnswhether the first date is prior to, is equal to, or islater than the second date.

DaysInMonth Pass a TDateTime value. Returns thenumber of days in the month associated with thepassed date.

DayOfTheMonth Pass a TDateTime parameter. Returnsthe number of days represented by this TDateTimesince the last day of the preceding month.

DaySpan Pass two TDateTime values. Returns thenumber of days between the two dates.

DecodeDayMonthWeek Pass a TDateTime parameterand four Word out parameters. The TDateTime isdecoded, and the four Word parameters are assignedthe year number, month number, week into the year,and day of the week, respectively.

EndOfAMonth Pass in a year number and a monthnumber, both as Word values. Returns a TDateTimevalue for the last day of the specified month.

EndOfAYear Pass a year number as a Word. Returns aTDateTime value for the last day of that year.

HourOfTheYear Pass a TDateTime value. Returns thenumber of hours into the year represented by thatTDateTime.

IncDay Pass in a TDateTime and some number of days.Returns a TDateTime specified number of days inthe future.

IncMinutes Same as IncDay, but returns a TDateTimethe specified number of minutes in the future.

IsInLeapYear Pass a TDateTime. Returns a Booleanvalue indicating whether or not the year in which theTDateTime resides in a leap year.

IsValidDate Pass a year number, month number, and daynumber. Returns a Boolean value indicating whetherthe numbers constitute a real date.

IsToday Pass in a TDateTime. Returns a Boolean valueindicating whether or not that TDateTime representsthe same date as the current date.

MinuteOfTheDay Pass a TDateTime parameter. Returnsthe number of minutes since midnight that thisTDateTime represents.

SameDate Pass in two TDateTime parameters. Returnsa Boolean value indicating whether or not those twoTDateTimes represent the same date.

StartOfAMonth Pass in a year and month number.Returns a TDateTime value that points to the firstday of that month.

StartOfAWeek Similar to StartOfAMonth, returns thefirst day of the week associated with the given date.This function is ISO 8601 compliant. More aboutthis is said later in his section.

WithinPastHours Pass two TDateTime values andsome number of hours. Returns a Boolean valueindicating if the two TDateTimes represent valueswithin the specified number of hours.

YearOf Pass in a TDateTime. Returns the year numberof the specified date.

This is just a small sampling of the routines that you willfind in the DateUtils unit. In addition, nearly every categoryof function has its equivalents for eachtime metric. For example, the following functions appearin the DateUtils unit: YearOf, MonthOf, WeekOf, DayOf,HourOf, MinuteOf, SecondOf, and MilliSecondOf.

Many of the functions in the DateUtils unit are ISO 8601compliant. As documented with comments at the beginningof the DateUtils unit, ISO 8601 specifies whatconstitutes the first week of the year, the first day of a month,and so on. ISO 8601compliant functions are flagged usingcomments in the interface section of the DateUtils unit.Foradditional information on ISO 8601, see http://www.iso.ch/iso/en/ISOOnline.frontpage and search for 8601.

Some of the functions and concepts used in the DateUtilsunit where contributed by Delphi developers, includingJeroen Pluimers, Glenn Crouch, Rune Moberg,and Ray Lischner. Thanks, guys.

The DateUtils.pas unit is located in the Source/Rtl/Common/DateUtils.pas file, located under the directory inwhich you installed Delphi 6 or later.

Did You Know? Delphi’s Date/

Time Functions

by Cary JensenThis information and much more is available on Cary’s bi-monthly, free Developer Days ELetter, to which you can signup on http://www.DeveloperDays.com .