Working with Dates and Times in Visual Basic

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Visual Basic Multidimensional ArraysWorking with Strings in Visual Basic

Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99

Buy eBook

In this chapter we will look at the Visual Basic Date 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 form 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 subtracted (such as day, year, hour, minute etc). The allowable values for this parameter are as follows:


ValueUnit of Time to Add/Subtract

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 above 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 two 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 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.

The following examples demonstrate possible uses of the DateDiff() function:

DateDiff(DateInterval.Year, #08/02/2006#, #08/02/2007# ) ' one year difference so returns 1 
DateDiff(DateInterval.Month, #08/02/2007#, #09/02/2007# ) ' one month difference so returns 1
DateDiff(DateInterval.Month, #08/02/2006#, #08/02/2007# ) ' one year difference so returns 12 (months) 

Accessing the System Date and Time from Visual Basic

One of the most common tasks related to dates and times in a Visual Basic application involves getting the current system date and time. This is achieved using the Visual Basic DateTime object. The DateTime object contains a number of properties, the most important of which are Now and Today. The Today property returns the current date held by the system. For example:

Dim objCurrentDate As Date = DateTime.Today

The Now property of the Visual Basic DateTime object contains both the date and the current system time. For example:

Dim objCurrentDate As Date = DateTime.Now

Checking if a Value is a Valid Date

Visual Basic provides the IsDate() function to ascertain whether a value is a valid date or not. This can be useful for checking that a user has entered a valid date into TextBox control. The IsDate() function takes a value as a parameter and returns True or False depending on whether the value is a valid date or not.

Purchase and download the fully updated Visual Basic 2010 edition of this eBook in PDF and ePub for only $9.99

Buy eBook