Conversion function 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 of time in Dos environment is different from the TdateTime type in VCL. When performing file operations, in order to maintain the consistency of time, you need to use the DateTimeToFileDate function for conversion. , the returned Integer value is the value used to describe time under Dos. DateTimeToSystemTime process: Definition: PRocedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime); Function: Convert a TDateTime type time to the TSystemTime type used by the Win API function, used when using the WinApi function to manipulate time. SystemTimeToDateTime function: Definition: function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime; Function: Convert a number of type TSysTemTime obtained in the WinApi function to type TDateTime. 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 value of year (year), month (month), and day (day), and return the date as TDateTime type, year range The value is 1-9999, the month range is 1-12, and the date range depends on the current month. If the entered value is out of the 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 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 hour (Hour), minute (min), The value of seconds (Sec) and microseconds (MSec) returns 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 (Hour), minutes (min), seconds (Sec), micro The value in seconds (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: SStrToDateTime 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 numerical 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 two-digit year representation method is used in the regional settings, the system will use the following method:
| current year | starting year | base value | 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 process: 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 effect. Just one is to get the output string through the shared variable, the value is in the Result, the other is to get the output string through the return value, the DateTime parameter is the date value you want to output, and the Format is composed of the format flag and the 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. If there are less than two digits, use 0 to supplement it (01-31) ddd : Display the current week number in abbreviated form. If Windows is the English version, it will be displayed as Mon-Sun. If it is the Chinese version, The display is the same as dddd. dddd: Display the current week number in complete format. If Windows is the English version, Monday-SumDay is displayed. If it is the Chinese version, Monday-Sunday is displayed. dddd: Output according to the short date format in the regional settings. 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, and use 0 to supplement the less than two digits (01-12) mmm: Use abbreviation to display the month name, the English version is displayed It is Jan-Dec, the Chinese version is the same as mmmm mmmm: Use the complete method to display the month name, the English version is displayed as January-December, the Chinese version is 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 : Display the hour as a one or two integer (0-23) hh : Display as a two-digit integer Hours, if there are less than two digits, use 0 to supplement them (00-23) n: Use one or two integers to display minutes (0-60) nn : Use two integers to display 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 supplement them (00-60) ss : Use two integers to display seconds, less than two digits Use 0 to complement (00-60) z: Use one to two integers to display the number of milliseconds (0-999) zzz: Use three integers to display the milliseconds, and use 0 to make up less than three digits (000-999) tt: Display the date according to the format in the regional settings am/pm: Used for 12-hour display, with AM means from 0 to 12 o'clock, and pm means 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 time-related variables: DateSeparator: Char date separator, used to separate years, months and days. TimeSeparator: Char time separator used to separate hours, minutes, seconds ShortDateFormat: String The definition of the short date format in the locale. LongDateFormat: Definition of long date format in String locale. ShortTimeFormat: Definition of short time format in String locale. LongTimeFormat: Definition of long time format in String locale. TimeAMString: String used to represent the morning string TimePMString: String used to represent the afternoon string ShortMonthNames: array[1..12] of String; used to abbreviate the array of month names, which is the mmm displayed when using FormatDateTime Identified string LongMonthNames: array[1..12] of String; an array used to fully represent the name of the month, which is the string identified by mmmm displayed when using FormatDateTime. ShortDayNames: array[1..7] of String; an array used to abbreviate the name of the week, which is when using FormatDateTime The string LongDayNames identified by ddd displayed: array[1..7] of String; An array used to fully represent the name of the week, which is the string identified by ddd displayed when using FormatDateTime. TwoDigitYearCenturyWindow: Word = 50; The starting year when using two-digit years.