Difference between revisions of "Working with Dates and Times in Visual Basic"
(→Accessing the Date and Time) |
(→Adjusting a Date or Time) |
||
Line 128: | Line 128: | ||
DateAdd(DateInterval.Week, -1, #8/2/2006#) | DateAdd(DateInterval.Week, -1, #8/2/2006#) | ||
</pre> | </pre> | ||
+ | |||
+ | == Retrieving Parts of a Date or Time == | ||
+ | |||
+ | The interval types listed in the ''DateAdd()'' section abovbe can be used with the Visual Basic ''DatePart()'' function to extract parts of a date or time. The syntax for the ''DatePart()'' function is as follows: | ||
+ | |||
+ | '''DatePart(''' ''interval'', date) As Integer | ||
+ | |||
+ | For example: | ||
+ | |||
+ | <pre> | ||
+ | DatePart(DateInterval.Month, #8/2/2007#) ' Returns 8 | ||
+ | DatePart(DateInterval.Day, #8/2/2007#) ' Returns 2 | ||
+ | </pre> | ||
+ | |||
+ | == Finding the Interval Between Two Dates or Times == | ||
+ | |||
+ | The difference between to dates or times can be determined using the Visual Basic ''DateDiff()'' function, the syntax for which is: | ||
+ | |||
+ | '''DateDiff(''' ''interval'', ''Date1'', ''Date2'') | ||
+ | |||
+ | The ''interval'' parameter specifies the unit of time to use when measuring the difference between two date and using the same values as those outlined for the ''DateAdd()'' function covered previously in this chapter. The two date values the represent the dates to compared. The function returns the difference as the number of specified units. For example, if the interval is defined as ''DateInterval.Day'' and the two dates are a week apart, ''DateDiff()'' will return 7. |
Revision as of 19:37, 2 August 2007
In this chapter we will look at the Visual Basic data type. In addition to providing a place to store a date, many tasks can be performed on a Date variable, such as adding or subtracting time, comparing dates and obtaining the System date and time. Each of these topics areas will be covered in detail in this chapter.
Creating and Initializing a Visual Basic Date
A Visual Basic Date can be created in the same way any other variable is created. For example the following Visual Basic code excerpt creates a Date variable:
Dim dteAppointment As Date
The Date value is initialized using a date string in the from mm/dd/yyyy encapsulated in hash (#) characters. For example to set the date to August 2, 2007:
Dim dteAppointment As Date = #8/2/2007#
A Date variable may be set to a specific time using the format hh:mm:ss AM/PM. For example, to set the time to 1:02 PM:
Dim dteAppointment As Date = #1:02:00 PM#
To set both the date and time:
Dim dteAppointment As Date = #8/2/2007 1:02:00 PM#
Accessing the Date and Time in a Date Variable
The simplest way to obtain the current setting of a Date variable is simply to reference the Date as you would any other variable, using the variable name. For example, the following Visual Basic code excerpt will display the current setting of the Date variable in a MessageBox:
Dim dteAppointment As Date = #8/2/2007 1:02:00 PM# MsgBox (dteAppointment)
Formatting Dates and Times
The default format of the Date and Time in Visual Basic is not always acceptable. For this reason, Visual Basic provides the Format() function to allow the format of the date to be controlled. The Format() function is actually a diverse function that can be used to format various types of data (such as string, monetary values and numbers). The Format() function uses the following syntax:
Format( value, style )
The value represents the data to be formatted. The style parameter defines how the data is to be formatted. In terms of formatting dates, a number of pre-defined styles are available.
The month can be formatted using sequences of 'M' characters. For example:
Format(#8/2/2007#, "MM") - returns Month as 2 digits (i.e. 08) Format(#8/2/2007#, "MMM") - returns abbreviated Month name (i.e. Aug) Format(#8/2/2007#, "MMMM") - returns full Month name (i.e. August)
Similarly the day of the month may be formatted using the 'd' character:
Format(#8/2/2007#, "d") - returns day of month as 1 (day < 10) or 2 digits (day > 9) (i.e. 2 or 12) Format(#8/2/2007#, "dd") - returns day of month as 2 digits (i.e. 02 or 12) Format(#8/2/2007#, "ddd") - returns abbreviated day of week (i.e Tue) Format(#8/2/2007#, "dddd") - returns unabbreviated day of week (i.e Tuesday)
The 'y' character is used to control the formatting of the year:
Format(#8/2/2007#, "y") - returns year as 1 digit (i.e. 7) Format(#8/2/2007#, "yy") - returns year as 2 digits (i.e. 07) Format(#8/2/2007#, "yyyy") - returns year as 4 digits (i.e. 2007)
The format of the time may be formatted using 'h', 'm', 's' and 'tt' characters:
Format(#1:02:00 PM#, "hh:mm:ss tt") - returns as 01:02:00 PM Format(#1:02:00 PM#, "hh:mm:ss") - returns as 01:02:00 Format(#1:02:00 PM#, "hh:mm") - returns as 01:02 Format(#1:02:00 PM#, "h:mm") - returns as 1:02
The format styles may be combined in any order to create the desired format. For example:
Format (#8/2/2007 1:02:00 PM#, "MMMM, d, yyyy, hh:mm") - Returns August, 2, 2007, 01:02
Adjusting a Date or Time
The value of a Date variable can be adjusted either backwards or forwards using the Visual Basic DateAdd() function. The Syntax of DateAdd() is as follows:
DateAdd( interval, number, date) As Date
The interval parameter specifies the unit of time to be added or suibtracted (such as day, year, hour, minute etc). The allowable values for this parameter are as follows:
Value | Unit of Time to Add/Subtract |
---|---|
DateInterval.Day | Day |
DateInterval.DayofYear | Day |
DateInterval.Hour | Hour |
DateInterval.Minute | Minute |
DateInterval.Month | Month |
DateInterval.Quarter | Quarter |
DateInterval.Second | Second |
DateInterval.Weekday | Day |
DateInterval.WeekOfyear | Week |
DateInterval.Year | Year |
For example, the following code will return a date one month into the future from the specified date:
DateAdd(DateInterval.Month, 1, #8/2/2006#)
The following code excerpt returns a date one week previous to the specified date:
DateAdd(DateInterval.Week, -1, #8/2/2006#)
Retrieving Parts of a Date or Time
The interval types listed in the DateAdd() section abovbe can be used with the Visual Basic DatePart() function to extract parts of a date or time. The syntax for the DatePart() function is as follows:
DatePart( interval, date) As Integer
For example:
DatePart(DateInterval.Month, #8/2/2007#) ' Returns 8 DatePart(DateInterval.Day, #8/2/2007#) ' Returns 2
Finding the Interval Between Two Dates or Times
The difference between to dates or times can be determined using the Visual Basic DateDiff() function, the syntax for which is:
DateDiff( interval, Date1, Date2)
The interval parameter specifies the unit of time to use when measuring the difference between two date and using the same values as those outlined for the DateAdd() function covered previously in this chapter. The two date values the represent the dates to compared. The function returns the difference as the number of specified units. For example, if the interval is defined as DateInterval.Day and the two dates are a week apart, DateDiff() will return 7.