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