傳回格式化為日期或時間的表示式。
語法
FormatDateTime(Date[,NamedFormat])
引數:
註:
設定值:NamedFormat 引數具有下列設定值:
vbGeneralDate:以一般日期格式取得日期,傳遞值為 0。
顯示日期和 (或) 時間。如果有日期部分,請將它顯示為簡短日期。如果有時間部分,請將它顯示為完整時間。如果有,則會顯示這兩個部分。
範例 1:
Dim d, mytime d="12/19/2024" mytime = FormatDateTime(d,0) 'Output: 12/19/2024 12:00:00 AM d="12/19/2024 02:30 PM" mytime = FormatDateTime(d,0) 'Output: - 12/19/2024 02:30:00 PM d="14:30:10 12/19/2024" mytime = FormatDateTime(d,0) 'Output: - 12/19/2024 02:30:10 PM d="02:30 PM 12/19/2024" mytime = FormatDateTime(d,0) 'Output: - 12/19/2024 02:30:00 PM d="12/19/2024 2pm" mytime = FormatDateTime(d,0) Output: - 12/19/2024 02:00:00 PM d="2 pm" mytime = FormatDateTime(d,0) 'Output: - 12/30/1899 02:00:00 PM d="12-19-2024" mytime = FormatDateTime(d,0) 'Output: - ERROR as delimiter should be same as locale’s delimiter
vbLongDate:取得完整日期,傳遞值 1。
使用電腦區域設定值中指定的完整日期格式來顯示日期。
註:
完整日期僅會以英文字元顯示內容,不支援工作日剖析。範例 2:
Dim d, mytime d="12/19/2024" mytime = FormatDateTime(d,1) 'Output: 12/19/2024 12:00:00 AM d="12/19/2024 02:30 PM" mytime = FormatDateTime(d,1) 'Output: - 12/19/2024 02:30:00 PM d="14:30:10 12/19/2024" mytime = FormatDateTime(d,1) 'Output: - 12/19/2024 02:30:10 PM d="02:30 PM 12/19/2024" mytime = FormatDateTime(d,1) 'Output: - 12/19/2024 02:30:00 PM d="12/19/2024 2pm" mytime = FormatDateTime(d,1) Output: - 12/19/2024 02:00:00 PM d="2 pm" mytime = FormatDateTime(d,1) 'Output: - 12/30/1899 02:00:00 PM d="12-19-2024" mytime = FormatDateTime(d,1) 'Output: - ERROR as delimiter should be same as locale’s delimiter
vbShortDate:取得簡短日期,傳遞值 2。
使用電腦區域設定值中指定的簡短日期格式來顯示日期。
範例 3:
Dim d, mytime d="12/19/2024" mytime = FormatDateTime(d,2) 'Output: 12/19/2024 12:00:00 AM d="12/19/2024 02:30 PM" mytime = FormatDateTime(d,2) 'Output: - 12/19/2024 02:30:00 PM d="14:30:10 12/19/2024" mytime = FormatDateTime(d,2) 'Output: - 12/19/2024 02:30:10 PM d="02:30 PM 12/19/2024" mytime = FormatDateTime(d,2) 'Output: - 12/19/2024 02:30:00 PM d="12/19/2024 2pm" mytime = FormatDateTime(d,2) Output: - 12/19/2024 02:00:00 PM d="2 pm" mytime = FormatDateTime(d,2) 'Output: - 12/30/1899 02:00:00 PM d="12-19-2024" mytime = FormatDateTime(d,2) 'Output: - ERROR as delimiter should be same as locale’s delimiter
vbLongTime:以完整時間格式取得時間,傳遞值 3。
使用電腦區域設定值中指定的時間格式來顯示時間。
註:
完整時間僅會以英文字元 (如 AM 或 PM) 顯示內容。範例 4:
Dim d, mytime d="12/19/2024" mytime = FormatDateTime(d,3) 'Output: 12/19/2024 12:00:00 AM d="12/19/2024 02:30 PM" mytime = FormatDateTime(d,3) 'Output: - 12/19/2024 02:30:00 PM d="14:30:10 12/19/2024" mytime = FormatDateTime(d,3) 'Output: - 12/19/2024 02:30:10 PM d="02:30 PM 12/19/2024" mytime = FormatDateTime(d,3) 'Output: - 12/19/2024 02:30:00 PM d="12/19/2024 2pm" mytime = FormatDateTime(d,3) Output: - 12/19/2024 02:00:00 PM d="2 pm" mytime = FormatDateTime(d,3) 'Output: - 12/30/1899 02:00:00 PM d="12-19-2024" mytime = FormatDateTime(d,3) 'Output: - ERROR as delimiter should be same as locale’s delimiter
vbShortTime:以簡短時間格式取得時間,傳遞值 4。
使用 24 小時格式 (hh:mm) 顯示時間。
範例 5:
Dim d, mytime d="12/19/2024" mytime = FormatDateTime(d,4) 'Output: 12/19/2024 12:00:00 AM d="12/19/2024 02:30 PM" mytime = FormatDateTime(d,4) 'Output: - 12/19/2024 02:30:00 PM d="14:30:10 12/19/2024" mytime = FormatDateTime(d,4) 'Output: - 12/19/2024 02:30:10 PM d="02:30 PM 12/19/2024" mytime = FormatDateTime(d,4) 'Output: - 12/19/2024 02:30:00 PM d="12/19/2024 2pm" mytime = FormatDateTime(d,4) Output: - 12/19/2024 02:00:00 PM d="2 pm" mytime = FormatDateTime(d,4) 'Output: - 12/30/1899 02:00:00 PM d="12-19-2024" mytime = FormatDateTime(d,4) 'Output: - ERROR as delimiter should be same as locale’s delimiter