FORMATDATETIME(date [,namedformat])

Returns the expression formatted as a date or time (String).


dateThe date to be formatted (Variant).
namedformat(Optional) A vbDateTimeFormat constant that specifies the format (Integer):
0 = vbGeneralDate (default)
1 = vbLongDate
2 = vbShortDate
3 = vbLongTime
4 = vbShortTime

REMARKS
* If "namedformat" is left blank, then 0 is used.
* This function was added in VB 6.0 and is slightly faster than using FORMAT
* You can use the FORMAT function to return a text string of a date in a particular format.
* You can use the FORMATCURRENCY function to return an expression formatted as a currency value.
* You can use the FORMATNUMBER function to return an expression formatted as a number.
* You can use the FORMATPERCENT function to return an expression formatted as a percentage.
* The equivalent .NET function is Microsoft.VisualBasic.Strings.FormatDateTime
* For the Microsoft documentation refer to learn.microsoft.com

Debug.Print FormatDateTime(Date + TimeValue("04:30:00"), vbDateTimeFormat.vbGeneralDate) '= 25/11/2021 04:30:00  
Debug.Print FormatDateTime(Date + Time(), vbDateTimeFormat.vbLongDate) '= 25 November 2021
Debug.Print FormatDateTime(Date, vbDateTimeFormat.vbLongDate) '= 25 November 2021
Debug.Print FormatDateTime(Date, vbDateTimeFormat.vbShortDate) '= 25/11/2021
Debug.Print FormatDateTime(Date + Time(), vbDateTimeFormat.vbLongTime) '= 17:31:15
Debug.Print FormatDateTime(Time(), vbDateTimeFormat.vbLongTime) '= 17:31:15
Debug.Print FormatDateTime(Time(), vbDateTimeFormat.vbShortTime) '= 17:32

Dim dtDate As Date
dtDate = #1/7/2022#
Debug.Print FormatDateTime(dtDate, vbGeneralDate) '= 07/01/2022
Debug.Print FormatDateTime(dtDate, vbLongDate) '= 07 January 2022
Debug.Print FormatDateTime(dtDate, vbShortDate) '= 07/01/2022

dtDate = #1/7/2022 12:30:20 PM#
Debug.Print FormatDateTime(dtDate, vbLongTime) '= 12:30:20
Debug.Print FormatDateTime(dtDate, vbShortTime) '= 12:30

© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited Top