Refer directly to the code snippet below, very simple very straightforward very convenient
var currenttime:tsystemtime; Year, month, day, hour, minute, second, millisecond:string; Datetime:string;begin GetSystemTime (currenttime); year:= IntToStr (currenttime.wyear); month:= IntToStr (currenttime.wmonth); day:= IntToStr (currenttime.wday); hour:= IntToStr (Currenttime.whour + 8); minute:= IntToStr (currenttime.wminute); second:= IntToStr (currenttime.wsecond); millisecond:= IntToStr (currenttime.wmilliseconds); Datetime:= year + '-' + month + '-' + day + ' + hour + ': ' + Minute + ': ' + second + ': ' + millisecond; ShowMessage (datetime); end;
It is important to note that
Hours of time problem
and the C-family language (such as C, C + +, PHP), this way to obtain the time, the year, month, day, minute, second, millisecond is, but the number of hours obtained is 8 hours late, such as the current time is 19:30, but using this method to get the number of hours is 11 points, So when it comes to development, this problem needs to be dealt with separately.
This is why the number of hours in the above code is added to 8: hour:= IntToStr (Currenttime.whour + 8);
Units that need to be introduced
To use the Tsystemtime type and the GetSystemTime procedure you need to reference the Windows unit.
Where the definition of tsystemtime is
Psystemtime = ^tsystemtime;_systemtime = Record Wyear:word; Wmonth:word; Wdayofweek:word; Wday:word; Whour:word; Wminute:word; Wsecond:word; Wmilliseconds:word;end; {$EXTERNALSYM _systemtime} Tsystemtime = _systemtime; SYSTEMTIME = _systemtime; {$EXTERNALSYM SYSTEMTIME}
Where GetSystemTime's statement is like this
Procedure GetSystemTime; External kernel32 name ' GetSystemTime ';
Its function prototype is
Procedure GetSystemTime (var lpsystemtime:tsystemtime); stdcall;
A look is the use of static loading of the DLL, so in Windows is only introduced GetSystemTime process, and did not implement, the specific implementation is in the Kernel32.dll this DLL implementation. The specific DLL is the use of C or Delphi language implementation is unknown, and so I later study to say. (It is estimated to be implemented in C, or why the number of hours acquired is 8 hours, as in C, but this is just the current speculation)
What is Kernel32.dll?
Kernel32.dll is a very important 32-bit dynamic link library file in Windows 9x/me, which belongs to the kernel-level file. It controls the system's memory management, data input and output operations, and interrupt handling, and when Windows starts, Kernel32.dll resides in memory-specific write-protected areas, making it impossible for other programs to occupy this area of memory.
How to use Delphi to get the current time, accurate to milliseconds