VBA-格式化 DateTime 函式



一種函式,可幫助開發人員對有效的日期和時間表達式進行格式化並返回其結果。

語法

FormatDateTime(date,format) 

引數說明

  • Date - 必需引數。

  • Format - 可選引數。指定要使用的日期或時間格式的值。它可以採用以下值。

    • 0 = vbGeneralDate - 預設

    • 1 = vbLongDate - 返回日期

    • 2 = vbShortDate - 返回日期

    • 3 = vbLongTime - 返回時間

    • 4 = vbShortTime - 返回時間

示例

新增一個按鈕並新增以下函式。

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   

執行上述函式後,將產生以下輸出。

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
vba_date_time.htm
廣告
© . All rights reserved.