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:
This will output the date and time as such:
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:
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:
This will output 17/7/2012.
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:
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:
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:
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:
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:
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:
This gets the time in 12 hour format, and also displays AM or PM.
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.
This will simply get the hour, no minutes or seconds.
As before, we can also add on hours, like in the following example:
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
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.