Generally speaking, there are several data structures used to describe time in Delphi, and the operation of time is essentially the operation of these structures.
TDateTime type:
The most commonly used data type representing date and time in Delphi is the TDateTime type. Just like an ordinary integer, you can define a date variable for the date to operate in the program. The TdateTime type is essentially a Double type number. The TdateTime type is defined in Delphi like this: type TDateTime = type Double. The specific algorithm is to use the integer part of the Double number to represent the date, with December 30, 1989 as the base point. Add and subtract in days, for example, the number 1 represents January 1, 1900, and the number -1 represents December 29, 1989. The decimal part is used to express time, and its value is the ratio of the time to be expressed to the total time of the whole day. For example, 6 o'clock in the morning is 6/24=0.25, and 6:15 in the morning is (6*60+15)/ (24*60)=0.260416666666666666666666666667. Below, several examples are given to illustrate the interaction between the TDateTime type and the Double type.
0 12/30/1899 00:00:00
2.75 1/1/1900 18:00:00
-1.25 12/29/1899 6:00:00
35065 1/1/1996 00:00:00
I believe that everyone can understand the conversion method between TdateTime and Double. Therefore, the operation of TDateTime is fundamentally the same as that of Double.
TTimeStamp type:
This is a structure type used to describe date and time. It is defined in Delphi like this:
type TTimeStamp = record Time: Integer; {The number of milliseconds from 0:00 midnight to the specified time} Date: Integer; {The number of days from today to the specified date} end;
Compared with TDateTime, it can express high-precision time more intuitively, and is generally used for millisecond-level operations and for expressing longer times.
PsystemTime type:
A pointer structure used for time operations in WinApi. His definition is:
type PSystemTime = ^TSystemTime; TSystemTime = record wYear: Word; wMonth: Word; wDayOfWeek: Word; wDay: Word; wHour: Word; wMinute: Word; wSecond: Word; wMilliseconds: Word; end;
This structure is mostly used when calling WinApi operations.
After understanding the essence of various time types, I believe everyone has a set of ideas for manipulating these types of time. However, Delphi also provides a complete set of process functions for manipulating time. These processes and functions are defined in SysUnit. In the unit, I will now introduce to you the relevant functions in Delphi:
Functions for time manipulation
Date function:
Definition: Date: TDateTime;
Function: Return the current date
example:
CurrentDate := Date;
DayOfWeek function:
Definition: function DayOfWeek(Date: TDateTime): Integer;
Function: Get the day of the week value of the specified date and return 1 to 7, representing Sunday to Saturday.
IncMouth function:
Definition: function IncMonth(const Date: TDateTime; NumberOfMonths: Integer): TDateTime;
Function: Find the date of the given date Data after NumberOfMonths months.
example:
Date1 := IncMonth(date,10);
If today is 2002-5-3, then Date1 is 2003-3-3
IsLeapYear function:
Definition: function IsLeapYear(Year: Word): Boolean;
Function: Determine whether the specified year is a leap year;
example:
if isLeapYear(2000) then ShowMessage('This year is a leap year');
Now function:
Definition: function Now: TDateTime;
Function: Used to obtain the current date and time
example:
CurrentDateTime := Now;
ReplaceDate procedure
Definition: procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime);
Function: Use the date part of parameter Newdate to replace the date part of parameter DateTime, but do not change the time part.
ReplaceDate procedure
Definition: procedure ReplaceTime(var DateTime: TDateTime; const NewTime: TDateTime);
Function: Use the time part of parameter Newdate to replace the time part of parameter DateTime, but do not change the date part.
If the above two processes are used together, it is equivalent to assignment.
Time function:
Definition: function Time: TDateTime;
Function: Return the current time
example:
CurrentTime := Time;
Conversion functions between various time types
DateTimeToFileDate function:
Definition: DateTimeToFileDate(DateTime: TDateTime): Integer;
Function: Convert a time of type TDateTime to time in Dos environment. The access method to time in Dos environment is different from the TdateTime type in VCL. When performing file operations, in order to maintain time consistency, you need to use the DateTimeToFileDate function. After conversion, the returned Integer value is the value used to describe time under Dos.
DateTimeToSystemTime procedure:
Definition: procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
Function: Convert a TDateTime type time to the TSystemTime type used by the Win API function, which is used when using the WinApi function to manipulate time.
SystemTimeToDateTime function:
Definition: function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
Function: Convert a TSysTemTime type number obtained in the WinApi function into a TDateTime type.
DateTimeToTimeStamp function:
TimeStampToDateTime function:
Definition: DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
Function: Used to convert between TDataTime type and TTimeStamp. TDataTime uses a double to describe a time, while TTimeStamp uses two integers to describe the time respectively. The difference between the two types can be found in the data type description section at the beginning of the article.
EncodeDate function:
Definition: function EncodeDate(Year, Month, Day: Word): TDateTime;
Function: Enter the year, month, and day values, and return the date as a TDateTime type. The year range is 1-9999, the month range is 1-12, and the date range is based on the current month. Depending on the situation, if the entered value is out of range, an EConvertError will be generated.
DecodeDate process:
Definition: procedure DecodeDate(Date: TDateTime; var Year, Month, Day: Word);
Function: Enter a date of type TDateTime and convert it into a year, month, and day value. If the input value is 0 or less than 0, then the year, month and day are all 0,
EncodeTime function:
Definition: EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime;
Function: Enter the value of hour (Hour), minute (min), second (Sec), microsecond (MSec), and return a time of type TDateTime, which is a decimal between 0 and 1. The value range of Hour is 0-23, the value range of Min is 0-59, the value range of Sec is 0-59, and the value range of MSec is 0-999. If the input value exceeds the range, an EConvertError is generated. mistake.
DecodeTime process:
Definition: procedure DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word);
Function: Enter a time and convert it into hours, minutes, seconds, and microseconds (MSec).
TDateTime type and string type conversion function:
DateTimeToStr function:
DateToStr function
TimeToStr function
Definition: function DateTimeToStr(DateTime: TDateTime): string;
function TimeToStr(Time: TDateTime): string;
function TimeToStr(Time: TDateTime): string;
Function: Convert a TDateTime type number into a string, DateTimeToStr converts date and time, DateToStr only converts date, and TimeToStr only converts time. The converted output effect is YYYY-MD H:M:S
StrToDateTime function
StrToDate function
StrToTime function
Definition: function StrToDateTime(const S: string): TDateTime;
function StrToDate(const S: string): TDateTime;
function StrToTime(const S: string): TDateTime;
Function: Convert a string with date and time format into TDateTime, where S must be a valid string, such as
YY-MM-DD HH:MM:SS format, otherwise the EConvertError event will be triggered and an error message will be prompted.
The string of the time part must be composed of 2 to 3 numeric strings, and separated by the delimiter characters set in the Windows regional settings. The format requirements must comply with the settings in the Windows regional settings, where HH, MM (hour, minute) must be added, SS (second) is optional, you can also add Am and Pm at the end to distinguish morning and afternoon. At this time, the system will think that the 12-hour representation is used, otherwise it will be considered that the 24-hour representation is used.
The format of the date part is required to comply with the short date format in Windows regional settings, which is also composed of 2 to 3 numerical strings. If there are only 2 numbers in the string, it is considered that the month and date are specified, and the year is the current year. ,
If the locale uses a two-digit year representation, the system will handle it as follows:
For the starting year base value of the current year, enter year 03. Enter year 50. Enter year 68.
1998 0 1900 1903 1950 1968
2002 0 2000 2003 2050 2068
1998 50 1948 2003 1950 1968
2000 50 1950 2003 1950 1968
2002 50 1952 2003 2050 1968
2020 50 1970 2003 2050 2068
2020 10 2010 2103 2050 2068
First, obtain the starting year of the two-digit year in the regional settings. For example, if the two-digit year range is set to 1932-2031 in the regional settings, the starting year is 32 years. If the starting year is 0, it is considered to be two digits. The year represents this century. If the year is actually greater than 0, the value of the current year minus the starting year is used. This value is called the base value. If it is greater than or equal to this value, it is considered to be this century, otherwise it is considered to be the next century. The following Give a few examples:
DateTimeToString procedure:
FormatDateTime function:
Definition: procedure DateTimeToString(var Result: string; const Format: string; DateTime: TDateTime);
function FormatDateTime(const Format: string; DateTime: TDateTime): string;
Function: Get the time string to be output by defining the format in the Format string. For example, if you want to output "Today is Friday, May 5, 2002", you can use these two methods. The two methods have the same function, but one The output string is obtained through shared variables. The value is in Result. The other is to obtain the output string through the return value. The DateTime parameter is the date value you want to output. Format is composed of a format flag and an additional string. The additional string is surrounded by "", just like the Printf function in C. For example, the Format value of "Today is Friday, May 5, 2002" is '" Today is "yyyy" year "mm" month "dd" day ," dddd', yyyy, mm, dd, dddd are all format identifiers. The explanations of various format identifiers are as follows:
d: Use one or two integers to display the day (1-31)
dd: Use two integers to display the day, and use 0 to make up for less than two digits (01-31)
ddd: Display the current week number in abbreviated form. If Windows is an English version, it will be displayed as Mon-Sun. If it is a Chinese version, it will be displayed as dddd.
dddd: Display the current week number in full format. If Windows is an English version, Monday-SumDay will be displayed. If it is a Chinese version, Monday-Sunday will be displayed.
ddddd : Output in the short date format in the locale.
dddddd : Output in the long date format in the locale.
m: Use one or two integers to display the month (1-12)
mm: Use two integers to display the month. If there are less than two digits, use 0 to make up (01-12)
mmm: Use abbreviation to display the month name. The English version is displayed as Jan-Dec, and the Chinese version is the same as mmmm.
mmmm: Use the complete method to display the month name. The English version displays January-December, and the Chinese version displays January to December.
yy: Display the year as a two-digit integer (00-99)
yyyy: Display the year as a four-digit integer (0000-9999)
h: Use one or two integers to display the hour (0-23)
hh: Use two integers to display the hour. If there are less than two digits, use 0 to make up (00-23)
n: Use one or two integers to display minutes (0-60)
nn: Use two integers to display the minutes, and use 0 to make up for less than two digits (00-60)
s: Use one or two integers to display seconds (0-60)
ss: Use two integers to display seconds, and use 0 to make up for less than two digits (00-60)
z: Display milliseconds with one to two integers (0-999)
zzz: Use three-digit integers to display milliseconds. If there are less than three digits, use 0 to make up (000-999)
tt : displays the date in the format in the locale
am/pm: used for 12-hour display, with AM representing from 0 to 12 o'clock, and pm representing from 12 to 0 o'clock.
Time-related variables:
Delphi encapsulates various information about local settings and defines a series of variables. Here are some variables related to time:
DateSeparator:Char
Date separator, used to separate year, month and day
TimeSeparator:Char
Time separator used to separate hours, minutes, seconds
ShortDateFormat: String
Definition of short date format in locale.
LongDateFormat: String
Definition of long date format in locale.
ShortTimeFormat: String
Definition of short time format in locale.
LongTimeFormat: String
Definition of long format in locale.
TimeAMString: String
A string used to represent morning
TimePMString: String
A string used to represent the afternoon
ShortMonthNames: array[1..12] of String;
The array used to abbreviate the month name is the string identified by mmm displayed when using FormatDateTime.
LongMonthNames: array[1..12] of String;
The array used to fully represent the month name is the string identified by mmmm displayed when using FormatDateTime.
ShortDayNames: array[1..7] of String;
The array used to abbreviate the day of the week name is the string identified by ddd displayed when using FormatDateTime.
LongDayNames: array[1..7] of String;
The array used to fully represent the name of the week is the string identified by ddd displayed when using FormatDateTime
TwoDigitYearCenturyWindow:Word = 50;
The starting year when using two-digit years.
Okay, now the part about time operations in Delphi is basically finished. Readers can also check the corresponding help files to solve the problems they encounter according to their own needs when using actual programming.