How to Use the FormatDateTime Function in Visual Basic

Written by roy huggins
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Use the FormatDateTime Function in Visual Basic
Displaying formatted times and dates is a simple task in Visual Basic. (digital clock image by JoLin from Fotolia.com)

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:
Moderate

Other People Are Reading

Instructions

  1. 1

    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.

  2. 2

    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.

  3. 3

    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.

  1. 1

    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."

  2. 2

    Run FormatDatTime with the output from CDate as your first parameter and your chosen format option number as the second parameter.

  3. 3

    Output the returned value from FormatDateTime using your favourite output function.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.