|
|
|
Date and Time Functions/Procedures 1. DateToStr function (unit: SysUtils) function DateToStr(Date: TDateTime): string; DateToStr converts a variable of type TDateTime to a formatted string. The conversion uses the format specified by the ShortDateFormat global variable. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := DateToStr(Now); end; 2. DateTimeToStr function (unit: SysUtils) function DateTimeToStr(DateTime: TDateTime): string; DateTimeToStr converts a variable of type TDateTime to a string. If DateTime parameter does not contain a date value, the date displays as 00/00/00. If the DateTime parameter does not contain a time value, the time displays as 00:00:00 AM.To change how the string is formatting, change some of the date and time typed constants. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := DateTimeToStr(Now); end; 3. Date function (unit: SysUtils) function Date: TDateTime; The Date function returns the current date. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'Today is ' + DateToStr(Date); end; 4. DayOfWeek function (unit: SysUtils) function DayOfWeek(Date: TDateTime): Integer; The DayOfWeek function returns the day of the week of the specified date as an integer between 1 and 7. Sunday is the first day of the week and Saturday is the seventh. Example: procedure TForm1.Button1Click(Sender: TObject); var ADate: TDateTime; begin ADate := StrToDate(Edit1.Text); Label1.Caption := 'Day ' + IntToStr(DayOfWeek(ADate)) + ' of the week'; end; 5. DecodeDate procedure (unit: SysUtils) procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word); The DecodeDate procedure breaks the value specified as the Date parameter into Year, Month, and Day values. If the given TDateTime value is less than or equal to zero, the year, month, and day return parameters are all set to zero. Example: procedure TForm1.Button1Click(Sender: TObject); var Present: TDateTime; Year, Month, Day, Hour, Min, Sec, MSec: Word; begin Present:= Now; DecodeDate(Present, Year, Month, Day); Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month ' + IntToStr(Month) + ' of Year ' + IntToStr(Year); DecodeTime(Present, Hour, Min, Sec, MSec); Label2.Caption := 'The time is Minute ' + IntToStr(Min) + ' of Hour ' + IntToStr(Hour); end; 6. DecodeTime procedure (unit: SysUtils) procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word); The DecodeTime procedure breaks the value specified as the Time parameter into hours, minutes, seconds, and milliseconds. Example: procedure TForm1.Button1Click(Sender: TObject); var Present: TDateTime; Year, Month, Day, Hour, Min, Sec, MSec: Word; begin Present:= Now; DecodeDate(Present, Year, Month, Day); Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month ' + IntToStr(Month) + ' of Year ' + IntToStr(Year); DecodeTime(Present, Hour, Min, Sec, MSec); Label2.Caption := 'The time is Minute ' + IntToStr(Min) + ' of Hour ' + IntToStr(Hour); end; 7. EncodeDate function (unit: SysUtils) function EncodeDate(Year, Month, Day: Word): TDateTime; The EncodeDate function returns a TDateTime type from the values specified as the Year, Month, and Day parameters. The year must be between 1 and 9999. Valid Month values are 1 through 12. Valid Day values are 1 through 28, 29, 30, or 31, depending on the Month value. For example, the possible Day values for month 2 (February) are 1 through 28 or 1 through 29, depending on whether or not the Year value specifies a leap year. If the specified values are not within range, an EConvertError exception is raised. The resulting value is one plus the number of days between 12/30/1899 and the given date. Example: procedure TForm1.Button1Click(Sender: TObject); var MyDate: TDateTime; begin MyDate := EncodeDate(83, 12, 31); Label1.Caption := DateToStr(MyDate); end; 8. EncodeTime function (unit: SysUtils) function EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime; EncodeTime returns a TDateTime type for a specified Hour, Min, Sec, and MSec. If the value of the Time24Hour typed constant is False, valid Hour values are 0 through 12. If the value of Time24Hour is True, valid Hour values are 0 through 23. Valid Min and Sec values are 0 through 59. Valid MSec values are 0 through 999. If the specified values are not within range, an EConvertError exception is raised. The resulting value is a number between 0 (inclusive) and 1 (not inclusive) that indicates the fractional part of a day given by the specified time. The value 0 corresponds to midnight, 0.5 corresponds to noon, 0.75 corresponds to 6:00 pm, etc. Example: procedure TForm1.Button1Click(Sender: TObject); var MyTime: TDateTime; begin MyTime := EncodeTime(0, 45, 45, 7); Label1.Caption := TimeToStr(MyTime); end; 9. FormatDateTime function (unit: SysUtils) function FormatDateTime(const Format: string; DateTime: TDateTime): string; FormatDateTime formats the date-and-time value given by DateTime using the format given by Format. The following format specifiers are supported:
Format specifiers may be written in upper case as well as in lower case letters--both produce the same result. If the string given by the Format parameter is empty, the date and time value is formatted as if a 'c' format specifier had been given. Example: The following example assigns 'The meeting is on Thursday, March 2, 2000, at 10:30 AM' to the string variable S. S := FormatDateTime('"The meeting is on" dddd, mmmm d, yyyy, ' + '"at" hh:mm AM/PM', StrToDateTime('02/03/00 10:30am')); 10. Now function (unit: SysUtils) function Now: TDateTime; This function returns the current date and time, corresponding to Date + Time. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'The date and time is ' + Str(Now); end; 11. StrToDate function (unit: SysUtils) function StrToDate(const S: string): TDateTime; The date in the string must be a valid date. The string must consist of two or three numbers, separated by the character defined by the DateSeparator global variable. The order for month, day, and year is determined by the ShortDateFormat global variable--possible combinations are m/d/y, d/m/y, and y/m/d. If the string contains only two numbers, it is interpreted as a date (m/d or d/m) in the current year. Year values between 0 and 99 are assumed to be in the current century. If the given string does not contain a valid date, an EConvertError exception is raised. Example: procedure TForm1.Button1Click(Sender: TObject); var ADate: TDateTime; begin ADate := StrToDate(Edit1.Text); Label1.Caption := DateToStr(ADate); end; 12. Time function (unit: SysUtils) function Time: TDateTime; The Time function returns the current time. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := 'The time is ' + TimeToStr(Time); end; 13. TimeToStr function (unit: SysUtils) function TimeToStr(Time: TDateTime): string; The TimeToStr function converts the Time parameter, a variable of type TDateTime, to a string. The conversion uses the format specified by the LongTimeFormat global variable. Change the format of the string display by changing the values of some of the date and time variables. Example: procedure TForm1.Button1Click(Sender: TObject); begin Label1.Caption := TimeToStr(Time); end; References: · Help file, Win32 Developer's References · Charles Calvert, Delphi Unleashed, Sams Publishing |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This article is written by Irhantoro E |