Dates and Times
One module in Visual Basic is DateAndTime . Which contains date values, time values, or date and time values. As a programmer, there are cases that we always display the date and time in our application or web application. Using visual basic we can manipulate dates and times in many ways. To do this, open and visual basic and create a new project and save it as “Datetime”. And add a button to our form, then double click it and add the following code.
- MsgBox(DateAndTime.Now) '10/21/2013 10;37:23 AM
- MsgBox(Date.Now.ToLongDateString) 'Monday, October 21, 2013
- MsgBox(Date.Now.ToShortDateString) '10/21/2013
- MsgBox(Date.Now.ToString("dddd")) 'Monday
- MsgBox(Date.Now.Year) '2013
- MsgBox(Date.Now.AddDays(30)) '11/20/2013 10;37:23 AM
- MsgBox(Date.Now.ToLongTimeString) '10;37:23 AM
- MsgBox(Date.Now.ToString("hh:mm:ss tt"))'10;37:23 AM
- MsgBox(Date.Now.Hour) '10
- MsgBox(Date.Now.Minute) '37
- MsgBox(Date.Now.Second) '23
The first line of code, it simply prints out the current Date and Time according to your PC.
10/21/2013 10;37:23 AM
Next line, it displays the long format of date as a string and we don’t display them because we did not specify it.
Monday, October 21, 2013
Next line, it displays the short format of date as a string.
The next line will simply print out the current day.
Then the next line of code will display the year. And you can do this also same on how we display the current day by specifying the MMMM instead of dddd for correct typecasting.
Then for the Next line of code, it adds thirty days so the out should look like as shown below.
11/20/2013 10;37:23 Am
Next, it displays the time in a long format.
This gets the time in 12 hour format and also displays AM and PM.
In the next line of code, we extract the hour.
And for Seconds
Take note, using Visual Basic are easy and there are many ways to manipulate the Dates and Time and this tutorial is just only serves as an eye opener to the wide world of programming.