FormatDateTime Functions
Returns an expression formatted as a date or time.
Syntax
FormatDateTime(Date[,NamedFormat])
Arguments:
- Date: Required. Date expression to be formatted.
Note:
- If string is provided as an input, then delimiters used in Input
Date and, or time should be same as system specific locale
delimiters. Special characters as shown in this image
are not supported.
- Number of days should not be given as an input argument, it’s not supported.
- Date, month & year should be written in complete format. For example- 08/02/2024, Abbreviated formats like 8/2/24 are not supported.
- If string is provided as an input, then delimiters used in Input
Date and, or time should be same as system specific locale
delimiters. Special characters as shown in this image
- NamedFormat: Optional. Numeric value that indicates the date and time format used. If omitted, vbGeneralDate is used. See Settings section for values.
Settings: The NamedFormat argument has the following settings:
vbGeneralDate: To get Date in General Date format pass Value as
0.
Display a date and, or time. If there is a date part, display it as a short date. If there is a time part, display it as a long time. If present, both parts are displayed.
Example 1:
Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,0)
'Output: 12/19/2024 12:00:00 AM
d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,0)
'Output: - 12/19/2024 02:30:00 PM
d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,0)
'Output: - 12/19/2024 02:30:10 PM
d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,0)
'Output: - 12/19/2024 02:30:00 PM
d="12/19/2024 2pm"
mytime = FormatDateTime(d,0)
Output: - 12/19/2024 02:00:00 PM
d="2 pm"
mytime = FormatDateTime(d,0)
'Output: - 12/30/1899 02:00:00 PM
d="12-19-2024"
mytime = FormatDateTime(d,0)
'Output: - ERROR as delimiter should be same as locale’s delimiter
vbLongDate: To get Long Date Pass Value 1.
Display a date using the long date format specified in your computer's regional settings.
Note:
Long date displays content only in English characters and weekday parsing is not supported.Example 2:
Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,1)
'Output: 12/19/2024 12:00:00 AM
d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,1)
'Output: - 12/19/2024 02:30:00 PM
d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,1)
'Output: - 12/19/2024 02:30:10 PM
d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,1)
'Output: - 12/19/2024 02:30:00 PM
d="12/19/2024 2pm"
mytime = FormatDateTime(d,1)
Output: - 12/19/2024 02:00:00 PM
d="2 pm"
mytime = FormatDateTime(d,1)
'Output: - 12/30/1899 02:00:00 PM
d="12-19-2024"
mytime = FormatDateTime(d,1)
'Output: - ERROR as delimiter should be same as locale’s delimiter
vbShortDate: To get Short Date Pass Value 2.
Display a date using the short date format specified in your computer's regional settings.
Example 3:
Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,2)
'Output: 12/19/2024 12:00:00 AM
d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,2)
'Output: - 12/19/2024 02:30:00 PM
d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,2)
'Output: - 12/19/2024 02:30:10 PM
d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,2)
'Output: - 12/19/2024 02:30:00 PM
d="12/19/2024 2pm"
mytime = FormatDateTime(d,2)
Output: - 12/19/2024 02:00:00 PM
d="2 pm"
mytime = FormatDateTime(d,2)
'Output: - 12/30/1899 02:00:00 PM
d="12-19-2024"
mytime = FormatDateTime(d,2)
'Output: - ERROR as delimiter should be same as locale’s delimiter
vbLongTime: To get Time in Long Time Format Pass Value 3.
Display a time using the time format specified in your computer's regional settings.
Note:
Long time displays content only in English characters like AM or PM.Example 4:
Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,3)
'Output: 12/19/2024 12:00:00 AM
d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,3)
'Output: - 12/19/2024 02:30:00 PM
d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,3)
'Output: - 12/19/2024 02:30:10 PM
d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,3)
'Output: - 12/19/2024 02:30:00 PM
d="12/19/2024 2pm"
mytime = FormatDateTime(d,3)
Output: - 12/19/2024 02:00:00 PM
d="2 pm"
mytime = FormatDateTime(d,3)
'Output: - 12/30/1899 02:00:00 PM
d="12-19-2024"
mytime = FormatDateTime(d,3)
'Output: - ERROR as delimiter should be same as locale’s delimiter
vbShortTime: To get Time in Short Time Format Pass Value 4.
Display a time using the 24-hour format (hh:mm).
Example 5:
Dim d, mytime
d="12/19/2024"
mytime = FormatDateTime(d,4)
'Output: 12/19/2024 12:00:00 AM
d="12/19/2024 02:30 PM"
mytime = FormatDateTime(d,4)
'Output: - 12/19/2024 02:30:00 PM
d="14:30:10 12/19/2024"
mytime = FormatDateTime(d,4)
'Output: - 12/19/2024 02:30:10 PM
d="02:30 PM 12/19/2024"
mytime = FormatDateTime(d,4)
'Output: - 12/19/2024 02:30:00 PM
d="12/19/2024 2pm"
mytime = FormatDateTime(d,4)
Output: - 12/19/2024 02:00:00 PM
d="2 pm"
mytime = FormatDateTime(d,4)
'Output: - 12/30/1899 02:00:00 PM
d="12-19-2024"
mytime = FormatDateTime(d,4)
'Output: - ERROR as delimiter should be same as locale’s delimiter