Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Loading
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 4: Data, Operations, and Built-In Functions
Last change: February 13 2016 18:47:36.
<<PreviousNext>>

Date/Time Functions

Date/Time functions are listed in the following table.

Function Use Code Example Result
Today * Returns current date as set in the computer Today Today
Now* Returns current date and time as set in the computer Now Current date and time
TimeOfDay* Returns current time as set in the computer TimeofDay Current time
Day(Date) Returns the day of the Specified date Day(#12/31/2001#) 31
Month(Date) Returns the month of The specified date Month(#12/31/2001#) 12
Year(Date) Returns the year of the Specified date Year(#12/31/2001#) 2001
DateSerial(year,month,day) Returns a date given the year, month, and day in the parameter DateSerial(2002, 5, 31) 5/31/2002
DateValue(Date) Returns a date given a string such as “02-28-1998” as the parameter DateValue(“May-31-2002”) 05/31/2002
DateAdd(interval, number, date) Returns a Date/Time value after adding the specified date by the specified number of intervals DateAdd(DateInterval.Month, 2,#03-31-2002#) 05/31/2002
* Technically, these three are properties of the System.DateTime object rather than functions. You can use them not only to return values, but also to set the date/time for your system.

Note that the Day function must be qualified by Microsoft.VisualBasic for the IDE to accept your code.
The DateAdd function is a versatile function in handling Date/Time data. You specify the interval parameter by using the DateInterval enumeration. For example, to indicate an interval in year, you code DateInterval.Year. The number parameter requires an integer, while date can be any Date/Time value (Note that a date literal must be enclosed by a pair of the # sign). For example, the following expression:
DateAdd(DateInterval.Second, 5, Now)
will return a Date/Time value that represents 5 seconds from now.
Look It Up
If you need to perform Date/Time calculations, use the keywords “DateAdd function” and “DateDiff function” to search the index tab of the help file.  These two topics give you specific details and fine points for various computations.

Formatting Date Data

Recall that you can use the Format function to format numeric data. You can also use the same function to format date/time. The following table shows a list of selected predefined date/time formats.

Format Name Meaning Code Example Result
General Date, or G Display a date and/or time Format(#12/31/2002#, “General Date”) 12/31/2002 12:00 AM
Long Date, or D Display a date in the computer’s long date format Format(#12/31/2002#, “Long Date”) Tuesday, December 31, 2002
Medium Date Display a date in the medium date format Format(#12/31/2002#, “Medium Date”) Tuesday, December 31, 2002
Short Date or d Display a date in the computer’s short date format Format(#12/31/2002#, “Short Date”) 12/31/2002
Long Time or T Display a time using the computer’s long time format, including hours, minutes, and seconds Format(#3:23:25 PM#, "Long time") 3:23:25 PM
Medium Time Display a time using the computer’s medium time format Format(#3:23:25 PM#, "Medium time") 3:23:25 PM
Short Time or t Display a time using the computer’s short time format Format(#3:23:25 PM#, "Short time") 3:23 PM

Look It Up
You can also use the FormatDateTime function to format date/time data. Use the keyword FormatDateTime to search in the index tab of the help file for information on how it can be used.

Of course, you can also create your own user-defined formatting string to format date/time. The following table gives a selected list of examples.

Code example Result Comment
Format(#2/23/2002#, “MM/dd/yyyy”) 02/23/2002 M for month, d for day, y for year; number of characters defines maximum number of output digits
Format(#2/23/2002#,”M/d/yy”) 2/23/02  
Format(#2/23/2002#,”d-MMM”) 23-Feb MMM specifies 3 characters for month
Format(#2/23/2002#,”MMMM yy”) February 02 MMMM specifies full month name
Format(#2:35:6 pm#, “HH:mm:ss”) 14:35:06 H for hour (24 hour clock), m for minute, s for seconds; number of characters defines maximum number of digits displayed
Format(#2:35:06 PM#, "h:m:s tt") 2:35:6 PM h for hour (12 hour clock); tt shows AM or PM

Look It Up
Use the keyword Format function to search the Index tab of the help file; then double-click user-defined dates/times under the format function heading for a complete list of characters for date/time formatting.

VB also provides financial functions that can be used to compute annuity, mortgage payments, and depreciation amounts. You can find these functions by searching the online Help file with the keyword Financial functions.

Last change: February 13 2016 18:47:36.
<<PreviousNext>>