VBA FormatDateTime()函數

FormatDateTime()函數它可以幫助開發人員格式化並返回有效的日期和時間表達式。

語法

FormatDateTime(date,format)

參數

  • Date - 一個必需的參數。
  • Format - 一個可選參數。指定要使用的日期或時間格式的值。它可以採取以下值 -
    • 0 = vbGeneralDate - 默認值
    • 1 = vbLongDate - 返回長日期
    • 2 = vbShortDate - 返回短日期
    • 3 = vbLongTime - 返回長時間
    • 4 = vbShortTime - 返回短時間

示例

添加一個模組,並添加以下示例代碼 -

Private Sub Constant_demo_Click()
   d = ("2018-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

執行上面示例代碼,得到以下結果 -

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

上一篇: VBA日期時間函數 下一篇: VBA數組