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.
  1. MsgBox(DateAndTime.Now) '10/21/2013 10;37:23 AM
  2. MsgBox(Date.Now.ToLongDateString) 'Monday, October 21, 2013
  3. MsgBox(Date.Now.ToShortDateString) '10/21/2013
  4. MsgBox(Date.Now.ToString("dddd")) 'Monday
  5. MsgBox(Date.Now.Year) '2013
  6. MsgBox(Date.Now.AddDays(30)) '11/20/2013 10;37:23 AM
  8. MsgBox(Date.Now.ToLongTimeString) '10;37:23 AM
  9. MsgBox(Date.Now.ToString("hh:mm:ss tt"))'10;37:23 AM
  10. MsgBox(Date.Now.Hour) '10
  11. MsgBox(Date.Now.Minute) '37
  12. 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. 10/21/2013 The next line will simply print out the current day. Monday 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. 2013 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. 10;37:23 AM This gets the time in 12 hour format and also displays AM and PM. 10;37:23 AM In the next line of code, we extract the hour. 10 For Minutes 37 And for Seconds 23 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.


Add new comment