Visual Basic 2010 (Console) Guide
The Date Structure

There are lots of situations where it necessary for programmers to work with dates or times. The Date structure contains many properties and methods to make this easier.

When you declare a variable of the type Date, it represents an instant in time, normally expressed as a date and time of day.

For example,

Dim now As Date = Date.Today
Console.WriteLine("Today's date is {0} ", now)
Dim currDateTime As Date = Date.Now
Console.WriteLine("The current date and time is {0} ", currDateTime)
Dim tomorrow As Date = currDateTime.AddDays(1)
Console.WriteLine("Tomorrow, it will be {0} ", tomorrow)
Dim backThen As Date = New Date(2000, 1, 1)
Console.WriteLine("The first day of they year 2000 was {0} ", backThen.DayOfWeek)

Type in and run the program above. Adapt the program to tell you what the day of the week was when you were born.

Creating A New DateTime

When you make a new instance of something like a date, Visual Basic uses the constructor function to make the new object. Different types have different constructors. For many, you have a choice of constructors that you want to use to set up your variable. When there is more than one constructor, we say that the constructor function is overloaded (and this is generally a good thing).

There are lots of ways to make a date or date+time variable. The following are normally all that you need,

Dim myDate As Date = New Date(year, month, day)
Dim myDateTime As Date = New Date(year, month, day, hour, minute, second)

DateTime Properties

The following properties are used to create and manipulate instances of DateTime structure.

DateGets the date component DateTime
DayGets the day component Integer
DayOfWeekGets the week dayDayOfWeek Enumeration
HourGets the hour componentInteger
MillisecondGets the millisecond componentInteger
MinuteGets the minute componentInteger
MonthGets the month componentInteger
NowGets a DateTime object that is the current date and timeDateTime
SecondGets the second componentInteger
TimeOfDayGets the time part of a DateTime instanceTimeSpan
TodayGets a DateTime object that is the current date and time set to 00:00:00DateTime
YearGets the year componentInteger

DateTime Methods

The following methods can be used to manipulate a DateTime instance. Look at how some of these are used in the example above.

AddDays(d)Adds d days to the DateTime
AddHours(h)Adds h hours to the DateTime
AddMilliseconds(m)Adds m milliseconds to the DateTime
AddMinutes(m)Adds m minutes to the DateTime
AddMonths(m)Adds m months to the DateTime
AddSeconds(s)Adds s seconds to the DateTime
AddYears(y)Adds y years to the DateTime
DaysInMonth(y, m)Returns an integer equal to the number of days in month m of year y.
IsLeapYear(y)Returns a Boolean (true or false)
ToShortDateString()Returns a short string representation of the date component of a DateTime
ToShortTimeString()Returns a short string representation of the time component of a DateTime
ToString()Returns a string representation of a DateTime

Having A Go

  1. Write a program that allows the user to type in a date and get the day of the week for that date.
  2. Write a program that tells the user whether a year entered at the keyboard is a leap year.