Visual Basic Dates and Times

As previously stated, dates and times in Visual Basic can be stored in a ‘Date’ variable. In its simplest form the current date and time can be assigned to a variable as follows.

Dim dateTime As Date = DateTime.Now

It is also possible to assign a specific date and time to a variable.

Dim dateTime As Date = New DateTime(2017, 2, 1, 5, 30, 0)

The values in the parenthesis refer the year, month, day, hours, minutes and seconds of the desired date and time, in this case 1 February 2017, along with a time of 5:30am.

If no formatting is applied, a ‘Date’ variable can be displayed, for example, in the console, in a similar manner to other variables, such as strings, as shown below, using the ‘WriteLine’ method.

Console.WriteLine(dateTime)

This will display in the console as follows. Note, this format may vary depending on the Operating System regional settings.

01/02/2017 05:30:00

Often, when the value of a ‘Date’ variable is used, this isn’t an ideal format. Visual Basic allows for a number of different formats to be used.

Console.WriteLine("Date: {0:dddd, dd MMMM yyyy}", dateTime)

The above example displays the day of the week in words, represented by ‘dddd’, followed by a comma, a two digit day, ‘dd’, the month in words, ‘MMMM’, and finally a four digit year, ‘yyyy’.

Date: Wednesday, 01 February 2017

Similarly, the format of the time portion can be specified.

Console.WriteLine("Time: {0:HH:mm}", dateTime)

Here, the time will be displayed with the hours, using a twenty four hour clock, represented by ‘HH’, followed by a colon and then two digits for the minutes, ‘mm’.

Time: 05:30

Note that, instead of specifying the format as part of the placeholder, the ‘ToString’ method of the actual variable can be used. This is split over two lines for display purposes only.

Console.WriteLine("Date and time: {0}",
    dateTime.ToString("dddd, dd MMMM yyyy, HH:mm"))

This combines the date and time formats above into one string.

Date and time: Wednesday, 01 February 2017, 05:30

Below is a table showing format patterns that can be used when displaying dates and times.

Format
Pattern
Description
d Day of the month as a number from 1 through 31.
dd Day of the month as a number from 01 through 31.
ddd Three character abbreviation for the day of the week e.g. Mon for Monday.
dddd Full name for the day of the week e.g. Monday.
h Hours, using a 12 hour clock, with no leading zero e.g. 5.
hh Hours, using a 12 hour clock, with a leading zero e.g. 05.
H Hours, using a 24 hour clock, with no leading zero e.g. 17.
HH Hours, using a 24 hour clock, with a leading zero e.g. 20.
m Minutes, with no leading zero e.g. 6.
mm Minutes, with a leading zero e.g. 06.
M Month as a number, with no leading zero e.g. 3 for March.
MM Month as a number, with a leading zero e.g. 03 for March.
MMM Three character abbreviation for the month e.g. Mar for March.
MMMM Full name for the month e.g. March.
s Seconds, with no leading zero e.g. 8.
ss Seconds, with a leading zero e.g. 08.
t Abbreviation for time of day, AM and PM e.g. A or P.
tt Time of day e.g. AM or PM.
y Year, without the century and no leading zero e.g. 9.
yy Year, without the century and a leading zero e.g. 09.
yyyy Year, including the century e.g. 2017.

As well as being able to format dates and times using the format patterns above, Visual Basic also provides a number of properties to access individual components of a date and time, as shown below.

Dim dateTime As Date = New DateTime(2017, 2, 1, 5, 30, 0)

Console.WriteLine(dateTime.Day)
Console.WriteLine(dateTime.Month)
Console.WriteLine(dateTime.Year)
Console.WriteLine(dateTime.Hour)
Console.WriteLine(dateTime.Minute)
Console.WriteLine(dateTime.Second)

On top of these properties, there are also built in methods for manipulating dates and times, as can be seen below, where one is added to each component of a date and time.

Dim dateTime As Date = New DateTime(2017, 2, 1, 5, 30, 0)

Console.WriteLine(dateTime)

dateTime = dateTime.AddDays(1)
dateTime = dateTime.AddMonths(1)
dateTime = dateTime.AddYears(1)
dateTime = dateTime.AddHours(1)
dateTime = dateTime.AddMinutes(1)
dateTime = dateTime.AddSeconds(1)

Console.WriteLine(dateTime)

The above will display the original date and time, followed by the date and time with one added to each component.

01/02/2017 05:30:00
02/03/2018 06:31:01

In order to subtract from the various components, a negative value can be used with the above methods, for example minus one to subtract one day.

The properties and methods discussed above are by no means exhaustive. There are many others available.