Get the current system time with Delphi

zhaozj2021-02-11  204

The current system time is often taken when developing applications. Although Y2K seems to have passed safe, but in our newly developed app, it is necessary to deal with "time" problem. Two ways to obtain current system time are introduced in "Believe in Delphi4.0 Battle Skills" (hereinafter referred to as "Book"), "the two methods are insufficient or wrong, but the following discussion will be discussed below. The first method of this book is to use the TIME () function to get the current system time, the return result is a variable of the TDATETIME structure type. For example: procedure tform1.button2click (sender: TOBJECT); VARDATETIME: TDATETIME; BegindateTime: = Time (); caption: = DATETOSTR (datetime) '' Timetostr (datetime); end; but no matter, its result is It is 99-12-30 xx: xx: xx, obviously the date is wrong. By analyzing the help of Delphi, Time () is used to return the correct "time-time second", Timetostr (DateTime), and should not be used to return "date". In fact, the system function used separately for the return date is Date. So what is the correct "time division" that can be returned to the correct "timeday"? You can use the now function, for example: procedure tform1.button1click (sender: Tobject); varmytime: tdatetime; beginmytime: = now CAPTION: = DATOSTR (MyTime) '' Timetostr (MyTime); // Directly with CAPTION: = DATETITOSTR (NOW); END; only 2 in the date format returned with NOW, that is, 2000 is shown in 000 This doesn't seem to be satisfactory. In addition, NOW and TIME can only get accurate to second time, in order to get more accurate millisecond times, you can use the API function getSystemTime, and its corresponding TSystemTime type is defined as: TsystemTime = Recordwyear : Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; Word; END; Obviously, it is also possible to easily use its knot in program logic? - Various types of time values, so use the function getSystemTime has great advantages. However, the usage of this function is wrong. By looking at the Windows SDK help, the function prototype is: Void getSystemTime (LPSystemTime LPST), the parameter pointer LPST acquisition system time, so you can implement it as follows: Procedure TFORM1.BUTTON3CLICK (Sender: TOBJECT); VARSYSTIME: TSYSTEMTIME; BegingeTsystemTime (SYSTIME); CAPTION: = INTOSTR (SYSTIME.WYEAR) '' INTOSTR (SYSTIME.WMONTH); // if systemime.wyear> 2000 Then // .... .. Using the acquired time value END in the program logic; comprehensive discussion above, obtain the current system time utilization function getSystemTime is relatively convenient and flexible.

转载请注明原文地址:https://www.9cbs.com/read-4247.html

New Post(0)