Be the first user to complete this post

  • 0
Add to List

VBA-Excel: Date-Time Functions – FormatDateTime()

Description:

The FormatDateTime function takes date as a parameter and returns the specified formatted date or time.

Format:

FormatDateTime(strDate[,strFormat])

 Arguments:
  • strDate
    • Manda­tory
    • Type: Date
    • Date which needs to be formatted.
  • strFormat
    • Optional
    • Type: Numeric
    • Numeric value which represents the format in which the date to be converted, if not provided any value, vbGeneralDate, 0 will be taken as default.
ConstantValue Description
vbGeneralDate

0

Default, Returns Date: mm/dd/yyyy and if time specified: hh:mm:ss PM/AM
vbLongDate

1

Returns Date: weekDay,month Name, Year
vbShortDate

2

Returns Date: mm/dd/yyyy
vbLongTime

3

Returns Time: hh:mm:ss PM/AM
vbShortTime

4

Returns Time: hh:mm

Example :

Function FnFormateDateTime()

   Dim strDate

   Dim strResult

   strDate = Now

   strResult = "General Format Date is: " & FormatDateTime(strDate) & vbCrLf

   strResult = strResult & "Long Format Date is: " & FormatDateTime(strDate, vbLongDate) & vbCrLf

   strResult = strResult & "Short Format Date is: " & FormatDateTime(strDate, vbShortDate) & vbCrLf

   strResult = strResult & "Long Format Time is: " & FormatDateTime(strDate, vbLongTime) & vbCrLf

   strResult = strResult & "Short Format Time is: " & FormatDateTime(strDate, vbShortTime) & vbCrLf

   MsgBox strResult

End Function
FormatDateTime()
FormatDateTime()



Also Read:

  1. VBA-Excel: Date-Time Functions – WeekDay() and WeekDayName()
  2. VBA-Excel: Date-Time Functions – CDate()
  3. VBA Excel – Refer to Multiple Ranges : Union Method
  4. VBA-Excel: Array Functions – Split()
  5. VBA-Excel : 3D-Ranges - Working with Multiple WorkSheets At The Simultaneously
  6. VBA-Excel: Date-Time Functions – Day()
  7. VBA-Excel: Array Functions – Join() – Converts Array to String
  8. VBA-Excel : Strings Functions – Right