Working with Dates and Times in Visual Basic

From Techotopia
Revision as of 19:04, 2 August 2007 by Neil (Talk | contribs) (New page: 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 ...)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

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

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(#8/2/2007 1:02:00 PM#, hh:mm:ss tt) - returns as 01:02:00 PM