Visual Basic Dates And Times

Dates and times are also another data type in Visual Basic. Sometimes you may need to print out the date in your application or web application, like for example when a post was last edited. There are many ways you can manipulate dates and times in Visual Basic, and we will discuss the most common ones. Create a new VB console application and name it Dates And Times. Then between Sub Main and End Sub() put the following code:

Console.WriteLine(DateAndTime.Now)
Console.ReadLine()

This will output the date and time as such:

17/07/2012 19:39:06

This will print out the date and time according to your PC. One thing you should bear in mind is that since this is based on the .Net Framework, the date and time will print out depending on the format the computer uses and the time zone you are in. The above code simply prints out the current date and time.

Getting the date

Here we will just focus on getting the date and then format it in several ways. We will do the same as before, but this time we will say Date.Now. Copy the following:

Console.WriteLine(Date.Now.ToLongDateString)
Console.ReadLine()

This prints out the date in a long format; it will not print out the time as we have not told it to. We can also print “short date” by using ToShortDateString. Try removing the ToLongDateString and replacing it with ToShortDateString, or simply copy the following:

Console.WriteLine(Date.Now.ToShortDateString)

This will output 17/7/2012.

Extract date

Here we will get just the month and day on their own. Since the date will be displayed as an integer, we will need to convert it to a string and format it; for example:

Console.WriteLine(Date.Now.DayOfWeek)

Now this surprisingly outputs 2 (meaning Tuesday ), because Tuesday is the second day of the week. However, if you want the word “Tuesday” you need to format it. What we do is write Day.Now.ToString(format) as  follows:

 Console.WriteLine(Date.Now.ToString("dddd"))

This will print out the current day. You can do the same for month by putting MMMM instead of dddd (case-sensitive). Getting the year is also quite simple; we use this code:

Console.WriteLine(Date.Now.Year)

This will print out the current year. Now we are going to focus on the AddDays Method, which adds days to the current day. For example, today is 17/07/2012. If you wanted to know what the day will be in 30 days, you would write:

Console.WriteLine(Date.Now.AddDays(30))

If today is 17 July 2012, in 30 days it will be 16 August 2012. This also works for AddHours, AddMinutes, AddYear, AddSeconds and so on.

Getting the time

Next we will get the time on its own. We repeat the same process as when we got the date, but this time we will use ToLongTimeString:

Console.WriteLine(Date.Now.ToLongTimeString)

This will print out the current time, and it will print out the hours, minutes and seconds. You can also use the ToShortTimeString method; this will only get the hour and minute. You also may want to get the time in a 12 hour clock format. To do this:

Console.WriteLine(Date.Now.ToString("hh:mm:ss tt"))

This gets the time in 12 hour format, and also displays AM or PM.

Extract time

Like the date we can also extract the time and get the hours and minutes on their own. The process is basically the same as above.

Hour

Console.WriteLine(Date.Now.Hour)

This will simply get the hour, no minutes or seconds.

Minutes

Console.WriteLine(Date.Now.Minute)

Seconds

Console.WriteLine(Date.Now.Second)

As before, we can also add on hours, like in the following example:

Console.WriteLine(Date.Now.AddHours(10).ToLongTimeString)

You can also get the minutes and seconds as before. In this example, after we have given the hours to advance by (which is 10), we also add which time format we want. This is necessary, because without it, it will print out the date and time.

Method / Property Name Description
ToLongDateString Prints out the day in the following format 17 July 2012
ToShortDateString Prints out the date in short format 17/7/2012
ToLongTimeString Gets the current time in the format HH:MM:SS (Hours, Minutes, Seconds)
ToShortTimeString Gets the current time in the format HH:MM (Hours and Minutes)
Now Gets the current date and time
Day Gets the current day as integer
Year Gets the year
Month gets the month as integer
DayofWeek Gets the day of the week as integer
Hour Gets the current hour
Minute Gets the current minute
AddHours Adds hours to the current time
AddDays Adds days to the current day
AddMonth Adds months to the current month
AddYear Adds years to the current year

For more information go to http://msdn.microsoft.com/en-us/library/03ybds8y

Summary

Dates and time are easy to use in Visual Basic, and this tutorial gave you a starting point. There are many ways you can format and manipulate the date. Date and time are a data type, so remember if you are going to do some calculations based on the date and time it is best to use DateTime.UtcNow.