How to Convert the Date Format
Visual Basic 6.0 includes a number of formatting options that allow the user to display and print dates in different ways. By using a few simple Visual Basic controls and conditional statements, you can create a little program that will illustrate these methods. This current example starts with today's date and displays different formats for the same date. Use the formatting directions to insert desired ways to display a date within any program you write.
Instructions
-
-
1
Create a new Visual Basic (VB) project using the "Standard EXE" template available when you click on "File" and then "New Project" in VB. Click on "File" and then "Save Project As." Save the form as "ConvertDate" and the project as "Convert Date".
-
2
Double click on the "ListBox" control in the Toolbox at the left of the screen to add it to the form. In the "Properties" list on the right of the screen, set the form "Height" property to 2600 and the "Width" to 4500. Drag the "ListBox" to the left side of the form and set the "Height" and "Width" properties to 1425 and 4725. Add three "Labels" to the form by double-clicking on the large letter "A" in the Toolbox three times. Arrange the labels so that "Label3" is the topmost item on the form. Put "Label1" and "Label2" in a row below the ListBox. Set the "Height" and "Width" property of "Label1" to 500 and 1800. Set the "Height" and "Width" of "Label2" to 800 and 3250. Clear the captions of "Label1" and "Label2" in their "Properties" section so they are blank.
-
-
3
Click "View" and then "Code" in the top level Visual Basic menu. Type the following code exactly as it appears.
Option Explicit
Private Sub Form_Load()
Label3.Caption="Highlight the format desired to display it."
Label1.Caption = "Today is " & Format(Now, "m/d/yyyy")
Label2.Visible = True
List1.AddItem "Full Date"
List1.AddItem "Abbreviated Date"
List1.AddItem "Day/Month"
List1.AddItem "Month/Year"
List1.AddItem "Month Date, Year (Day of the Week)"
List1.AddItem "Abbreviated Date plus Day, Week, and Quarter of the Year"
End Sub -
4
Type the following lines of code below those in Step 3:
Private Sub List1_Click()
Select Case List1.ListIndex
Case "0"
Label2.Caption = Format(Now, "dddd,mmmm dd,yyyy")
Case "1"
Label2.Caption = Format(Now, "m/d/yy")
Case "2"
Label2.Caption = Format(Now, "d-mmmm")
Case "3"
Label2.Caption = Format(Now, "mmmm-yy")
Case "4"
Label2.Caption = Format(Now, "mmm dd, yyyy (dddd)")
Case "5"
Label2.Caption = Format(Now, "mm/dd/yy (\d\a\y=y \w\e\e\k=ww \q\u\a\r\t\e\r=q)")
End Select
End Sub -
5
Press "F5" to run the program. If there are no problems, you can click "File" and then "Make Convert Date.exe" to create an executable version. If something does not appear correct, then press "F8" to step through the lines of code and isolate where a typo may have entered the lines of code. By examining the format instructions in Step 4 you will understand how date conversions are handled in Visual Basic.
-
1
Tips & Warnings
By adding time formats as listed in the second Reference you can add time formatting options to your code.
Substitute an "InputBox" for "Label1" to allow the user to pick a specific date to format.
The use of quotation marks and date type abbreviations in Step 4 are critical. Any slight error will throw off the results.
References
Resources
- Photo Credit e - wishes happy new year !!! image by ril from Fotolia.com