Date and time values are stored internally in a special format, but you don’t need to know the exact format. They are double-precision numbers: the integer part represents the date, and the fractional part represents the time. A variable declared as Date with a statement like the following can store both date and time values:
Dim expiration As Date The following are all valid assignments: expiration = expiration = expiration = "July 2, 2008" expiration = Today()Code language: VB.NET (vbnet)
By the way, the Today() function returns the current date and time, while the Now() function returns the current date. You can also retrieve the current date by calling the Today property of the Date data type: Date.Today.
The pound sign tells Visual Basic to store a date value to the expiration variable, just as the quotes tell Visual Basic that the value is a string. You can store a date as a string to a Date variable, but it will be converted to the appropriate format. If the Strict option is on, you can’t specify dates by using the Long date format (as in the third statement of this example).
The date format is determined by the Regional Settings (found in the Control Panel). In the United States, the format is mm/dd/yy. (In other countries, the format is dd/mm/yy.) If you assign an invalid date to a date variable, such as 23/04/2002, the statement will be underlined and an error message will appear in the Task List window. The description of the error is Date constant is not valid.
The Date data type is extremely flexible; Visual Basic knows how to handle date and time values, so you won’t have to write complicated code to perform the necessary conversions. To manipulate dates and times, use the members of the Date type, which are discussed in detail in Chapter “Handling Strings, Characters, and Dates”, or the date and time functions of VB 6, which are still supported by VB 2008.
You can also perform arithmetic operations with date values. VB recognizes your intention to subtract dates and it properly evaluates their difference. The result is a TimeSpan object, which represents a time interval. If you execute the following statements, the value 638.08:49:51.4970000 will appear in the Output window:
Dim d1, d2 As Date d1 = Now d2 =Code language: VB.NET (vbnet)
The value of the TimeSpan object represents an interval of 638 days, 8 hours, 49 minutes, and 51.497 seconds.
Data Type Identifiers
Finally, you can omit the As clause of the Dim statement, yet create typed variables, with the variable declaration characters, or data type identifiers. These characters are special symbols that you append to the variable name to denote the variable’s type. To create a string variable, you can use this statement:
Dim myText$Code language: VB.NET (vbnet)
The dollar sign signifies a string variable. Notice that the name of the variable includes the dollar sign — it’s myText$, not myText. To create a variable of a particular type, use one of the data declaration characters shown in the following table. (Not all data types have their own identifiers.)
Table 2.3 – Data Type Definition Characters
|%||Integer (Int32)||counter%, var%|
|&||Long (Int64)||population&, colorValue&|
Using type identifiers doesn’t help to produce the cleanest and easiest-to-read code. They’re relics from really old versions of BASIC, and if you haven’t used them in the past, there’s no really good reason to start using them now.