⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vel14.htm

📁 简单的说明如何使用VB,非常适合初学使用者,而且是用图表来解说的
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons vel14.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velp07.htm"><LINK REL="Previous" HREF="vel13.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons vel14.htm</H2><P ALIGN=LEFT><A HREF="vel13.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velp07.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E104" >What You'll Learn</A><LI><A HREF="#E68E105" >Getting the Date and Time</A><LI><A HREF="#E68E106" >Set the Date and Time From Visual Basic</A><LI><A HREF="#E68E107" >How Much Time Has Passed?</A><LI><A HREF="#E68E108" >Serial Dates and Times</A><LI><A HREF="#E68E109" >Formatting with Format()</A><LI><A HREF="#E68E110" >Homework</A><UL><LI><A HREF="#E69E94" >General Knowledge</A><LI><A HREF="#E69E95" >Find the Bug</A><LI><A HREF="#E69E96" >Extra Credit</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E20"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Lesson 7, Unit 14</B></FONT></CENTER></H1><BR><A NAME="E67E23"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Working with Dates, Times, and Formats</B></FONT></CENTER></H2><BR><BR><A NAME="E68E104"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>What You'll Learn</B></FONT></CENTER></H3><BR><UL><LI>Getting the date and time<BR><BR><LI>Setting the date and time from Visual Basic<BR><BR><LI>Determining how much time has passed<BR><BR><LI>Using date arithmetic<BR><BR><LI>Getting serial dates and times<BR><BR></UL><P>This unit completes your learning of Visual Basic's built-in functions by exploring the built-in date, time, and formatting functions. This is the first unit of the book in which you'll work directly with the variant data type even though you've seen references to Variant throughout the book.<BR><P>In addition to the functions, this unit discusses the several date and time statements that augment the work you'll do with date and time function values. Date and time values are critical for time keeping, transaction recording, and reporting, so you must be able to write programs that track and work with such values. Visual Basic supports one of the most comprehensive library of date and time functions, as you'll see in this unit.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>The most important part of learning many of the date and time values is learning how Visual Basic stores date and time values in memory.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The formatting power of Visual Basic is superb as well. You'll see the formatting tables that convert your program output to any format needed. Visual Basic supports date, time, string, and number formatting capabilities.<BR><BR><A NAME="E68E105"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Getting the Date and Time</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Now(), Date$() and Time$() functions (and their cousins, the Date() and Time() functions) look inside your computer's clock and calendar to retrieve the current date and time for your program's use.<BR><P>In ancient times (a little over eight years ago), computers couldn't remember the date and time if you turned them off. They've come a long way! Somebody thought of the brilliant idea of putting a battery inside the computer so that the computer would remember the date and time every time you powered on the machine. Of course, batteries had been in wrist watches for years before then, but computer makers were so busy trying to squeeze 360K of storage onto one of those little 5 1/4-inch floppy disks that they didn't get around to the battery for a while.<BR><P>Visual Basic gives your Windows programs access to the date and time values stored internally in your computer. Assuming that the date and time are set properly, you can assign the date and time function return values to variables or display those values in controls.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: 24-hour time</I> measures time from 0 to 24 hours.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Some of the time values returned from these functions return a 24-hour time. If you're used to expressing time values using a 12-hour a.m. or p.m. time, you'll have to get accustomed to the 24-hour time in which Visual Basic adds 12 to all time values after 12:59 PM. Therefore, 3:45 in the morning is 3:45, but 3:45 in the afternoon is 15:45.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Note: </B>You can use the Format() function to change any 24-hour time to a 12-hour clock using the AM and PM indicators if you want to.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The Now(), Date$(), Date(), Time$(), and Time() functions are functions that don't accept arguments. As such, Visual Basic removes the parentheses if you type them after these functions. Nevertheless, this book does use the parentheses after the functions to remind you that they are built-in functions.<BR><P>The Now() function returns both the date and time. Now() returns a Variant data type in the following format:<BR><BR><PRE><FONT COLOR="#000080"><I>mm</I>/<I>dd</I>/<I>yy</I> <I>hh</I>:<I>mm</I>:<I>ss</I> [AM][PM]</FONT></PRE><P>where <I>mm</I> is a 2-digit month, <I>dd</I> is a 2-digit day, and <I>yy</I> is a 2-digit year value. The time appears to the right of the date. The time section of Now()'s return value uses a 12-hour clock rather than the 24-hour clock used by Time$() and Time(). <I>hh</I> is a 2-digit hour, <I>mm</I> is a 2-digit minute value, and <I>ss</I> is a 2-digit second value. Just for your information, Visual Basic stores this complete value internally as a double-precision value because only the double-precision value is large enough to hold that much information. Even though Visual Basic stores the date and time internally in a double-precision value, Now() returns the value formatted as shown inside the variant data type.<BR><P>If you've set your computer's version of Windows to an international setting that requires a different date and time format, these functions return date and time values that match your country's setting.<BR><P>Date$() returns the system date string in the following format:<BR><BR><PRE><FONT COLOR="#000080">mm-dd-yyyy</FONT></PRE><P>Date() returns the value in the variant data type. The difference between Date() and Date$() is that Date() doesn't return leading zeros in day or month numbers less than 10, Date() doesn't append 19 to the year, and Date() inserts forward slashes instead of hyphens between the date values.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Given the closeness to the year 2000, use Date$() exclusively so that the full year is returned in case you still use your program when the century turns.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>Time$() returns the system time in a string data type in the following 24-hour format:<BR><BR><PRE><FONT COLOR="#000080">hh:mm:ss</FONT></PRE><P>where <I>hh</I> is the hour (from 00 to 23), <I>mm</I> is the minute (from 00 to 59), and <I>ss</I> is the second (from 00 to 59). Time() returns the system time in a variant data type in the following 12-hour format:<BR><BR><PRE><FONT COLOR="#000080"><I>hh</I>:<I>mm</I>:<I>ss</I> [AM] [PM]</FONT></PRE><P>where Time$() always returns either AM or PM after the time to indicate the time of day.<BR><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 14.1 contains several assignment statements whose remarks describe the date or time value returned from the functions.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The date and time functions return values set inside your computer so that you can use and display those values in your program. The function that you use, Now(), Date(), or Time() (or the Date$() and Time$() counterparts) return various date and time formats.<BR><P><FONT COLOR="#000080"><B>Listing 14.1. Accessing time and date values.</B></FONT><BR><PRE><FONT COLOR="#000080">1: lblNow.Caption = Now() ' 7/9/97 07:48 PM2: lblDate1.Caption = Date() ' 7/9/973: lblDate2.Caption = Date$() ' 07/09/19974: lblTime1.Caption = Time() ' 07:48:24 PM5: lblTime2.Caption = Time$() ' 19:48:24</FONT></PRE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The assignment statements assume that the current date and time is July 9, 1997 at 7:48:24 in the evening. The different formats offer you the choice of how you want the date and time values displayed in the target labels.<BR><BR><A NAME="E68E106"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Set the Date and Time From Visual Basic</B></FONT></CENTER></H3><BR><P><FONT COLOR="#FF8000"><B><I>Concept: </I></B></FONT>The Date and Time statements (not the functions) enable you to change the computer's date and time values from within a Visual Basic application. The date and time values remain set until you change them again, either through subsequent Date and Time statements or through DOS or Windows commands.<BR><P>The DOS DATE and TIME commands enable you to check and set your computer's date and time settings. The Windows Control Panel program inside the Main program group also enables you to set these values from within Windows. Be sure to check your computer's date and time every month or so to make sure they're accurate.<BR><P>Visual Basic includes the Date and Time statements that set the date and time values of your computer's clock and calendar. Be sure to keep the parentheses off the Date and Time statements, or Visual Basic will think that you're improperly using the corresponding functions.<BR><P>Here are the formats of the Date and Time statements:<BR><BR><PRE><FONT COLOR="#000080">Date[$] = dateExpression</FONT></PRE><P>and<BR><BR><PRE><FONT COLOR="#000080">Time[$] = timeExpression</FONT></PRE><P>As with the corresponding functions, there are two versions of each statement, and the dollar sign distinguishes between the versions. If you don't specify the trailing dollar sign, you must enter the <I>dateExpression</I> as an unambiguous date value, and the <I>dateExpression</I> must be a string or date data type. All of the following set the computer's current date to July 9, 1998:<BR><PRE><FONT COLOR="#000080">Date = 7/9/1997Date = 07/9/97Date = July 9, 1997Date = Jul 9, 1997Date = 9-Jul-1997Date = 9 July 1997Date = 9 July 97</FONT></PRE><P>The <I>dateExpression</I> must contain a valid date between January 1, 1980 and December 31, 2099, or Visual Basic generates an error.<BR><P>If you do specify the trailing dollar sign (as in Date$), you can enter the <I>dateExpression</I> only in the following formats:<BR><PRE><FONT COLOR="#000080">Date$ = 07-9-97Date$ = 7-9-1998Date$ = 7/9/97Date$ = 7/9/1997</FONT></PRE><P>Owing to the many date formats, Visual Basic recognizes just about any way that you're used to specifying the date.<BR><P>If you don't specify the trailing dollar sign, you can enter the <I>timeExpression</I> as either a 12-hour clock or a 24-hour clock with quotation marks, as follows:<BR><BR><PRE><FONT COLOR="#000080">Time = &quot;7:48 PM&quot;</FONT></PRE><P>or<BR><BR><PRE><FONT COLOR="#000080">Time = &quot;19:48&quot;</FONT></PRE><P>If you do specify the trailing dollar sign (as in Time$), you can enter the <I>timeExpression</I> in any of these formats:<BR><PRE><FONT COLOR="#000080">hhhh:mmhh:mm:ss</FONT></PRE><P>You must use a 24-hour clock value when using <I>Time$</I>.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Tip: </B>Using these Time$ formats, you change only what you want to change. If your time zone has just turned to daylight savings time, for example, you can change just the hour.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Stop and Type: </I></B></FONT>Listing 14.2 contains a section of code that enables the user to change both the date and time.<BR><P><FONT COLOR="#FF8000"><B><I>Review: </I></B></FONT>The Date, Date$, Time, and Time$ statements enable you to change the computer's internal date and time settings. Those settings remain in effect until you or the user changes them again.<BR><P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -