FormatDateTime 函式

傳回格式化為日期或時間的表示式。

語法

FormatDateTime(Date[,NamedFormat])

引數:

  • Date必要。要格式化的日期表示式。

    註:

    • 如果提供字串作為輸入,則輸入日期和 (或) 時間中使用的分隔符號應與系統特定的地區設定分隔符號相同。不支援此影像 亂碼字元 中顯示的特殊字元。
    • 不應將天數指定為輸入引數,不支援此情況。
    • 應以完整格式撰寫日期、月份及年度。例如 - 08/02/2024,不支援 8/2/24 這類縮寫格式。
  • NamedFormat選擇性。表示所使用日期與時間格式的數值。如果省略,則會使用 vbGeneralDate。如需相關值,請參閱「設定值」區段。

設定值: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