The summary of the time operation method in Delphi _delphi

Source: Internet
Author: User
Tags current time date1 numeric locale month name

Generally speaking, there are several data structures used in Delphi to describe time, while the operation of time is essentially the operation of these structures.

Tdatetime Type:

The most commonly used data type in Delphi, Tdatetime type, is the same as the normal number of plastic, you can define a date variable for the date to operate in the program. The Tdatetime type is essentially a double number, which in Delphi defines the Tdatetime type: type Tdatetime = Type Double, and the specific algorithm is to use the integer part of the double number to represent the date. The reference point for December 30, 1989 is to add and subtract in days, such as the number 1 means January 1, 1900, and the number 1 means December 29, 1989. The decimal part is used to denote time, and its value is the ratio of the time to the total day, such as 6 in the morning is 6/24=0.25, and the Morning 6:15 is (6*60+15)/(24*60) = 0.2604166666666666666666666666667, below, give a few examples to illustrate the interaction between Tdatetime type and 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 am sure that you will understand the conversion method between Tdatetime and double, so fundamentally the tdatetime operation is identical to double.

Ttimestamp Type:
This is a type of structure used to describe the date and time, which is defined in Delphi:

Type 
Ttimestamp = record 
Time:integer; {The number of milliseconds from 0 o'clock midnight to the specified time} 
Date:integer; {Number of days on the day as the benchmark to the pointing date} 
End 

Compared with tdatetime, it is more intuitive to represent high precision time, typically for millisecond operations and for longer periods of time.

Psystemtime Type:
A pointer-type structure 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 used to invoke the WINAPI operation more than once.
After understanding the nature of various types of time, I believe we all have a set of operating these types of time ideas, but in Delphi also provides a set of operating time of the process functions, these processes and functions are defined in the Sysunit this unit, now to introduce the relevant functions Delphi:

Functions for TIME operations

Date Function:
Definition: date:tdatetime;
Function: Returns the current date
Example:

currentdate: = Date; 

DayOfWeek Function:
Definition: function DayOfWeek (date:tdatetime): Integer;
Function: Gets the week value of the specified date, returns 1~7, representing Sunday to Saturday.

Incmouth function:
Definition: function incmonth (const date:tdatetime; Numberofmonths:integer): Tdatetime;
Function: To find the date of the given date data in 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 years;
Example:

If Isleapyear (then) showmessage (' Year of Leap Years '); 

Now function:
definition: function now:tdatetime;
Function: Used to get the current date time
Example:

Currentdatetime: = Now; 

Replacedate process
Definition: Procedure replacedate (var datetime:tdatetime; const newdate:tdatetime);
Role: Replaces the date part of the parameter datetime with the parameter Newdate date part, but does not change the time portion.

Replacedate process
Definition: Procedure replacetime (var datetime:tdatetime; const newtime:tdatetime);
Action: Replaces the time part of the parameter datetime with the parameter newdate time section, but does not change the date part.
If the above two processes are used together, it is equivalent to a value assignment.

Time function:
definition: function time:tdatetime;
Function: Returns the current time
Example:

CurrentTime: = time; 

Conversion functions between various time types

datetimetofiledate function:
Definition: Datetimetofiledate (datetime:tdatetime): Integer;
Function: Converts a tdatetime type of time into a DOS environment, and the access method for time in DOS is not the same as the Tdatetime type in VCL, in order to maintain the consistency of time in the file operation, You need to convert using the Datetimetofiledate function, and the value of the integer returned is the value in DOS that describes the time.

Datetimetosystemtime Process:
Definition: Procedure datetimetosystemtime (datetime:tdatetime; var systemtime:tsystemtime);
Role: Converts a tdatetime type of time to the type of tsystemtime used by the win API function, which is used when manipulating time using the WINAPI function.

Systemtimetodatetime function:
Definition: function systemtimetodatetime (const systemtime:tsystemtime): Tdatetime;
Function: Converts a number of tsystemtime types obtained in the WINAPI function to the tdatetime type.

datetimetotimestamp function:
timestamptodatetime Function:
definition: Datetimetotimestamp (datetime:tdatetime): Ttimestamp;
function Timestamptodatetime (const timestamp:ttimestamp): Tdatetime;
Role: Used to convert between Tdatatime type and Ttimestamp. Tdatatime uses a double to depict a time, whereas Ttimestamp uses two reshaping numbers to describe the time separately. The two types of differences refer to 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 the year, month (month), Days (day), return the date to the Tdatetime type, the range of the year is 1-9999, the range of the month is 1-12, and the range of the date depends on the condition of the month, if the value entered is out of range, A econverterror error is generated.

Decodedate Process:
Definition: Procedure decodedate (Date:tdatetime; var year, Month, Day:word);
Role: Enter a date for the Tdatetime type, converting it to the value of the year, month (Month), Day (days). If the input value is 0 or less than 0, the daily average is 0.

Encodetime function:
definition: Encodetime (Hour, Min, Sec, Msec:word): Tdatetime;
function: Input hours (Hour), minutes (min), sec (sec), microseconds (MSEC) values, return a tdatetime type of time, the value is a decimal between 0 to 1. The value range of hour is 0-23,min range of 0-59,sec is a value range of 0-59,msec is 0-999, if the input value is out of range, then a econverterror error is generated.

Decodetime Process:
Definition: Procedure decodetime (Time:tdatetime; var Hour, Min, Sec, Msec:word);
Function: Enter a time to convert it to hours (Hour), minutes (min), sec (sec), microsecond (MSEC) values.

Tdatetime type and string conversion functions:

datetimetostr function:
datetostr function
Timetostr function
Definition: function datetimetostr (datetime:tdatetime): string;
function Timetostr (time:tdatetime): string;
function Timetostr (time:tdatetime): string;
Function: Converts a number of tdatetime types to strings, datetimetostr conversion dates and times, datetostr only dates, Timetostr converts only time. The output after conversion is yyyy-m-d 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: Converts a string with datetime format to Tdatetime, where s must be a valid string, such as
YY-MM-DD HH:MM:SS format, otherwise triggers the Econverterror event, prompting for an error message.
The strings of the time section must be a numeric string of 2~3, delimited by a separator character set in the Windows locale, with a format that conforms to the setting in the Windows locale where hh,mm (hours, minutes) must be added, SS (seconds) optional, You can also add am and PM to the back of the afternoon, when the system will assume that the 12-hour notation, or the use of 24-hour notation.
The format requirement for the date section conforms to the short date format in the Windows locale and is also composed of a numeric string of 2~3, if there are only 2 digits in the string, the month and date are specified, and the year is the current year.
If a two-bit year representation method is used in a locale, the system will handle the following methods:
Current year start 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, in the locale, you get the starting year of the two-bit year, if the two-bit year range in the locale is 1932-2031, the starting year is 32, and if the starting year is 0, the two-digit year is the century, and if the year is greater than 0, the current year minus the value of the starting year is used, This value is called a datum value, which is greater than or equal to the value of this century, otherwise considered to be the next century, here are a few examples to illustrate:

datetimetostring Process:
FormatDateTime function:
Definition: Procedure datetimetostring (var result:string; const format:string; Datetime:tdatetime);
function FormatDateTime (const format:string; Datetime:tdatetime): string;
Function: To output a time string by defining the format in the form string, such as to output "Today is May 5, 2002, Friday" can be used in these two ways, the same effect, but a shared variable to get the output string, the value in result, One is to get the output string by the return value, the datetime parameter is the date value that you want to output, and format is combined with the formatting label and the additional string. Additional strings are circled with "", just like the printf function in C. If "Today is May 5, 2002, Friday" The format value is ' Today is ' yyyy ' year "MM" month "DD" Day, "dddd ', YYYY,MM,DD,DDDD are format ID, The various format identifiers are explained as follows:
D: Display the day with one or two-digit integers (1-31)
DD: Displays days with two-bit integers, less than two digits in 0 replenishment (01-31)
DDD: Displays the current weekday number by thumbnail, if Windows is an English version, displays as Mon-sun, and if it is in Chinese, displays the same dddd.
DDDD: Displays the current weekday number in full, if Windows is in English, displays Monday-sumday, and if it is in Chinese, Monday ~ Sunday
DDDDD: Output in the Short date format in the locale.
DDDDDD: Output in a long date format in regional settings.
M: Displays the month with one or two-bit integers (1-12)
MM: Display month with two-bit integer, less than two-digit 0 complement (01-12)
MMM: Use the thumbnail display month name, the English version displays as Jan-dec, Chinese version with mmmm
Mmmm: Displays the month name in the full way, the English version displays as January-december, the Chinese version is January ~ December
YY: Display year by two-bit integer (00-99)
YYYY: Display year by four-bit integer (0000-9999)
H: Display hours with one or two-bit integers (0-23)
HH: Displays hours with two-bit integers, less than two bits in 0 replenishment (00-23)
N: Displays minutes with one or two-bit integers (0-60)
nn: minutes with two-bit integers, less than two digits in 0 replenishment (00-60)
S: Displays the number of seconds with one or two-bit integers (0-60)
SS: Two-bit integer display seconds, less than two digits in 0 replenishment (00-60)
Z: Displays the number of milliseconds (0-999) with a bit to two-bit integer
ZZZ: Use three-bit integer to display the number of milliseconds, less than three bits of 0 complement (000-999)
TT: Displays a date in the format of a locale
AM/PM: For a 12-hour display, with AM is the ~12 point from 0 Point, PM Representative from 12 point ~0 point.

Time-dependent variables:

The

Delphi encapsulates various information about locale and defines a series of variables with this, which describes some of the variables related to time:

Dateseparator:char
Date separator, used to separate month and day

The Timeseparator:char
Time separator, which is used to delimit the definition of short date format in hours, minutes, seconds

shortdateformat:string
locales. The definition of the long date format in the

longdateformat:string
Locale. The definition of a short time format in the

shorttimeformat:string
Locale. The

longtimeformat:string the definition of a long time format in the
Regional setting. The

timeamstring:string
used to represent the morning string

timepmstring:string
is used to represent the afternoon string

Shortmonthnames:ar RAY[1..12] of String;
The array that is used to represent the name of the month, which is the string of the MMM identity that is displayed when using FormatDateTime

longmonthnames:array[1..12] of string;
The array that is used to fully represent the name of the month, that is, the mmmm identifier string

shortdaynames:array[1..7] of string that is displayed when using FormatDateTime;
The array that is used to abbreviated the name of the week, which is the string

longdaynames:array[1..7] of the DDD identity that is displayed when using FormatDateTime;
The array that is used to fully represent the name of the week, which is the string of DDD identities displayed when using FormatDateTime

Twodigityearcenturywindow:word = 50; The
start year when the two-bit year is used.

Well, so far in Delphi about the time operation of the basic finished, the reader in the actual programming when the use of their own needs to see the corresponding help file to solve problems encountered.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.