Date variables store date values that may include a time part (or not), and they are declared with the Date data type:
Dim expiration As Date
The following are all valid assignments:
Dim expiration As Date expiration = #1/1/2010# Console.WriteLine(expiration) expiration = #8/27/1998 6:29:11 PM# Console.WriteLine(expiration) expiration = "July 2, 2011" Console.WriteLine(expiration) expiration = Today() Console.WriteLine(expiration)
01/01/2010 12:00:00 AM 27/08/1998 6:29:11 PM 02/07/2011 12:00:00 AM 18/10/2013 12:00:00 AM
Now and Today
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.
The format of the date inside the pound characters is determined by the regional settings (found in 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/2012, 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.
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 Time Span object, which represents a time interval.
Dim d1, d2 As Date d1 = Now d2 = #1/1/2004# Console.WriteLine(d1 - d2)