返回一个格式化为日期或时间的表达式。
语法
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