Visual Basic is simple to use, flexible and integrated into a wide array of Microsoft products such as Excel and Access. VB is one of the most popular programming languages in the world. It is often used in customised spreadsheets and other applications requiring "quick and dirty" programming tweaks. For this reason, it has been designed to easily execute common tasks such as calculating date math and displaying times and dates. When you want to display a date properly formatted for local date and time conventions, the FormatDateTime function in Visual Basic is your tool.
- Skill level:
Other People Are Reading
Feed the FormatDateTime function a valid date or time. You may be using input from a user that cannot be trusted and may require a certain amount of preformatting to insure the integrity of the data. To validate your input, run the IsDate function with the input as the only parameter.
Check the value returned from IsDate. If it is not "true," then your input is not a valid date or time in the local context. For example, the text "We like cheese" is not a date or time and would cause IsDate to return a "false" value. Also, "January 22" is not a date in the Japanese language. On an English language server, "January 22" is a legitimate date. If IsDate reports that the input is not a date or time, stop processing and return an error.
Convert your validated input to a date format that FormatDateTime can understand by running the CDate function with your input as the only parameter. Use the value returned from CDate in the following steps.
Choose the format in which you want to output your date and/or time. FormatDateTime provides five options.
Option "0" will make some decisions for you. If your input only had a date, it will return a date text in the local version of the format "mm/dd/yy." If your input contained only a time, it will return a time string in the local version of the format "hh:mm:ss PM/AM." If your input contained both, it will return both items in the above format.
Option "1" will return a date in the local version of the format "day-of-the- week, month-name year."
Option "2" will return a date in the local version of the format "mm/dd/yy."
Option "3" will return a time in the local version of the format "hh:mm:ss PM/AM."
Option "4" will return a time in 24-hour format. For example: "hh:mm."
Run FormatDatTime with the output from CDate as your first parameter and your chosen format option number as the second parameter.
Output the returned value from FormatDateTime using your favourite output function.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for