VBA – Format DateTime Function

  • Post author:
  • Post category:VBA
  • Post comments:0 Comments
date and time

A Function, which helps the developers to format and return a valid date and time expression.

Syntax

FormatDateTime(date,format) 

Parameter Description

  • Date − A required parameter.
  • Format − An optional parameter. The Value that specifies the date or time format to be used. It can take the following values.
    • 0 = vbGeneralDate – Default
    • 1 = vbLongDate – Returns date
    • 2 = vbShortDate – Returns date
    • 3 = vbLongTime – Returns time
    • 4 = vbShortTime – Returns time

Example

Add a button and add the following function.

Private Sub Constant_demo_Click()
   d = ("2013-08-15 20:25")
   msgbox("Line 1 : " & FormatDateTime(d))
   msgbox("Line 2 : " & FormatDateTime(d,1))
   msgbox("Line 3 : " & FormatDateTime(d,2))
   msgbox("Line 4 : " & FormatDateTime(d,3))
   msgbox("Line 5 : " & FormatDateTime(d,4))
End Sub   

When you execute the above function, it produces the following output.

Line 1 : 15/08/2013 8:25:00 PM 
Line 2 : Thursday, 15 August 2013
Line 3 : 15/08/2013
Line 4 : 8:25:00 PM
Line 5 : 20:25

Previous Page:-Click Here

Leave a Reply