📄 faq27.htm
字号:
<HTML>
<HEAD>
<TITLE>Determine the current date and time</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3>
Determine the current date and time
</H3>
<P>
The VCL <TT>TDateTime</TT> class is the easiest way to get the
current time. The code samples below show you how. The first block of code shows
how to retrieve the current time and date. The remaining code samples show some
other uses for <TT>TDateTime</TT>.
</P>
<pre>
<font color="navy">//---------------------------------------------------------------------</font>
<font color="navy">// TASK 1: Fetching the current date and time.</font>
<font color="navy">// You can build a mini-clock by adding this code to an OnTimer event.</font>
<font color="navy">// Set the timer interval between 100 and 250 for best results.</font>
<font color="navy">// Note: The static CurrentDateTime method of TDateTime returns a</font>
<font color="navy">// TDateTime object for the current system time and date.</font>
<font color="navy">// The TimeString and DateString functions convert the object</font>
<font color="navy">// to an AnsiString.</font>
Label1<b>-></b>Caption <b>=</b> TDateTime<b>:</b><b>:</b>CurrentDateTime<b>(</b><b>)</b><b>.</b>TimeString<b>(</b><b>)</b><b>;</b>
Label2<b>-></b>Caption <b>=</b> TDateTime<b>:</b><b>:</b>CurrentDateTime<b>(</b><b>)</b><b>.</b>DateString<b>(</b><b>)</b><b>;</b>
<font color="navy">//---------------------------------------------------------------------</font>
<font color="navy">// TASK 2: Setting an interval timeout value.</font>
<font color="navy">// First, declare a constant TDateTime object that represents an</font>
<font color="navy">// interval of 1000 seconds. TDateTime has quite a few different</font>
<font color="navy">// constructors. This one initializes the hours, minutes, seconds,</font>
<font color="navy">// and milliseconds values.</font>
<b>const</b> TDateTime OneThousandSeconds <b>=</b> TDateTime<b>(</b><font color="blue">0</font><b>,</b> <font color="blue">16</font><b>,</b> <font color="blue">40</font><b>,</b> <font color="blue">0</font><b>)</b><b>;</b>
<font color="navy">// now create a TDateTime object for the current time</font>
TDateTime CurrentTime<b>(</b>TDateTime<b>:</b><b>:</b>CurrentDateTime<b>(</b><b>)</b><b>)</b><b>;</b>
<font color="navy">// add the interval to the current time. This is too easy!</font>
TDateTime EndTime <b>=</b> CurrentTime <b>+</b> OneThousandSeconds<b>;</b>
<font color="navy">// finally, add code that checks to see if the interval has expired.</font>
<b>if</b><b>(</b>TDateTime<b>:</b><b>:</b>CurrentDateTime<b>(</b><b>)</b> <b>></b> EndTime<b>)</b>
Application<b>-></b>MessageBox<b>(</b><font color="blue">"Times up class. Put down your pencils."</font><b>,</b>
<font color="blue">"Times up"</font> <b>,</b> MB_OK<b>)</b><b>;</b>
<font color="navy">//---------------------------------------------------------------------</font>
<font color="navy">// TASK 3: Set an interval of 60 days from the current date.</font>
<font color="navy">// First, store the current date in a TDateTime object.</font>
TDateTime CurrentDate<b>(</b>TDateTime<b>:</b><b>:</b>CurrentDate<b>(</b><b>)</b><b>)</b><b>;</b>
<font color="navy">// add 60 days to the Current date</font>
TDateTime EndDate <b>=</b> CurrentDate <b>+</b> <b>(</b><b>double</b><b>)</b><font color="blue">60</font><b>;</b>
<font color="navy">// finally, add some code that checks to see if the interval has expired.</font>
<b>if</b><b>(</b>TDateTime<b>:</b><b>:</b>CurrentDateTime<b>(</b><b>)</b> <b>></b> EndDate<b>)</b>
Application<b>-></b>MessageBox<b>(</b><font color="blue">"You evaluation period is over. This "</font>
<font color="blue">"software will self destruct in 15 seconds."</font><b>,</b>
<font color="blue">"Demo period over"</font><b>,</b>MB_OK<b>)</b><b>;</b>
<font color="navy">//---------------------------------------------------------------------</font>
<font color="navy">// TASK 4: Create a TDateTime object from a string.</font>
<font color="navy">// First, we need some strings. You could provide the string with an edit</font>
<font color="navy">// box, but I will just declare an AnsiString to save time.</font>
AnsiString TimeString <b>=</b> <font color="blue">"10/02/97 13:15"</font><b>;</b> <font color="navy">// 1:15 pm</font>
<font color="navy">// Now create the TDateTime object by passing the string in the constructor</font>
<font color="navy">// Note: you could also use the StrToDate, StrToTime, or StrToDateTime</font>
<font color="navy">// functions in the RTL. In fact, this constructor just calls one of</font>
<font color="navy">// these three RTL calls. Also note that this constructor allows</font>
<font color="navy">// a second arg for limiting the string to just a date or just a time,</font>
<font color="navy">// but I was unable to find the enum type for this arg in the includes</font>
TDateTime ArmyTime<b>(</b>TimeString<b>)</b><b>;</b> <font color="navy">// create an object that represents</font>
<font color="navy">// the day I got out of the Army.</font>
<font color="navy">// Now do something with the object</font>
TDateTime ElapsedFreedomTime <b>=</b> TDateTime<b>:</b><b>:</b>CurrentDate<b>(</b><b>)</b> <b>-</b> ArmyTime<b>;</b>
<b>unsigned</b> <b>short</b> years<b>,</b> months<b>,</b> days<b>;</b>
ElapsedFreedomTime<b>.</b>DecodeDate<b>(</b><b>&</b>years<b>,</b> <b>&</b>months<b>,</b> <b>&</b>days<b>)</b><b>;</b>
years <b>-</b><b>=</b> <font color="blue">1900</font><b>;</b> <font color="navy">// years starts at 1900</font>
AnsiString msg <b>=</b> <font color="blue">"Elapsed time since I left the army (whoopeeee!!): "</font> <b>+</b>
IntToStr<b>(</b> years<b>)</b> <b>+</b> <font color="blue">" years, "</font> <b>+</b>
IntToStr<b>(</b> months<b>)</b> <b>+</b> <font color="blue">" months, and "</font> <b>+</b>
IntToStr<b>(</b> days<b>)</b> <b>+</b> <font color="blue">"days."</font><b>;</b>
Application<b>-></b>MessageBox<b>(</b>msg<b>.</b>c_str<b>(</b><b>)</b><b>,</b> <font color="blue">"Joy"</font><b>,</b>MB_OK<b>)</b><b>;</b>
</pre>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -