Working with Dates and Times in Visual Basic

From Techotopia
Revision as of 19:26, 2 August 2007 by Neil (Talk | contribs) (Accessing the Date and Time)

Jump to: navigation, search

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.


Contents


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:

ValueUnit of Time to Add/Subtract
DateInterval.DayDay
DateInterval.DayofYearDay
DateInterval.HourHour
DateInterval.MinuteMinute
DateInterval.MonthMonth
DateInterval.QuarterQuarter
DateInterval.SecondSecond
DateInterval.WeekdayDay
DateInterval.WeekOfyearWeek
DateInterval.YearYear

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#)