Delphi gets current system time (using API function GetSystemTime)

Source: Internet
Author: User
Tags what date

It is often necessary to obtain the current system time when developing an application. Although Y2K seems to be safe, the "time" issue should be handled with caution in our newly developed application.

In the "Mastery--delphi4.0 Combat Skills" (hereinafter referred to as "the book") on page 89th specifically describes two ways to get the current system time, but both of these methods are insufficient or error, the following discussion.

The first method of the book is to use the time () function to obtain the current system times and return the result as a variable of the tdatetime struct type. For example:

Procedure Tform1.button2click (Sender:tobject);

Var

Datetime:tdatetime;

Begin

Datetime:=time ();

CAPTION:=DATETOSTR (DateTime) + ' +timetostr (datetime);

End

But no matter what date, the result is 99-12-30 xx:xx:xx, the date is obviously wrong. By analyzing the help of Delphi, Time () is used to return the correct "times-hours-seconds", which is Timetostr (DateTime), and should not be used to return "date". In fact, a system function that is used alone to return a date is date.

So what is the right time to return the correct "hours and minutes" and return to the correct "month Day"? You can use the now function, for example:

Procedure Tform1.button1click (Sender:tobject);

Var

Mytime:tdatetime;

Begin

Mytime:=now;

Caption:=datetostr (mytime) + ' +timetostr (mytime);

or directly with Caption: = Datetimetostr (now);

End

The date format returned with now is only 2 bits in middle age, that is, 2000 is shown as 00, which does not seem satisfactory. Also now and time can only get accurate to seconds, in order to get a more accurate millisecond time, you can use the API function GetSystemTime, which corresponds to the definition of the Tsystemtime type:

Tsystemtime = Record

Wyear:word;

Wmonth:word;

Wdayofweek:word;

Wday:word;

Whour:word;

Wminute:word;

Wsecond:word;

Wmilliseconds:word;

End

Obviously, in the program logic can also easily use its structure into the---a variety of time values, so the use of function GetSystemTime has great advantages. But the use of the function in the book is wrong, and by looking at the Windows SDK Help, the function prototype is:

VOID GetSystemTime (Lpsystemtime lpst), the parameter pointer lpst gets the system time, so it can be implemented as follows:

Procedure Tform1.button3click (Sender:tobject);

Var

Systime:tsystemtime;

Begin

GetSystemTime (SysTime);

Caption:=inttostr (systime.wyear) + ' +inttostr (systime.wmonth);

If systime.wyear>2000 Then

// ...... Take advantage of the various time values obtained in the program logic

End

Based on the above discussion, it is convenient and flexible to obtain the current system time utilization function GetSystemTime.

http://blog.csdn.net/yanjiaye520/article/details/6259153

Delphi gets current system time (using API function GetSystemTime)

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.