VBA FormatDateTime Function: The FormatDateTime function in VBA returns the result as a string after applying a date and/or time format to the supplied expression.

Syntax

Parameter

Expression (specified) – This parameter represents the expression that you want to format.

NamedFormat (optional) – This parameter specifies the format that is to be applied to the Expression. The default value is set to vbGeneral.

It can take the following values:

  • vbGeneral (default)– It displays a date and/or time as defined in your system’s General Date setting.
  • vbLongDate- It displays a date as defined in your system’s Long Date settings
  • vbLongTime – It displays a time as defined in your system’s Long Time settings.
  • vbShortDate – It displays a date as defined in your system’s Short Date settings.
  • vbShortTime – It displays a time as defined in your system’s Short Time settings.

Return

This function returns a string value after applying a date and/or time format to the supplied expression.

Example 1

Output

12:00:00 AM
10/10/2020
VBA FormatDateTime Function

Example 2

Output

12:00:00 PM
12/30/1899
VBA FormatDateTime Function

Example 3

Output

11:00:00 AM
10/10/2020
VBA FormatDateTime Function

Example 4

Output

VBA FormatDateTime Function

Pin It on Pinterest

Share This