FormatDateTime 函数 [VBA]

将日期与/或时间格式应用到日期表达式,并将结果以字符串形式返回。

warning

This constant, function or object is enabled with the statement Option VBASupport 1 placed before the executable program code in a module.


语法:


        FormatDateTime (Date As Date [, NamedFormat As Integer])
    

返回值:

String

参数:

Date: The date expression to be formatted.

NamedFormat: 可选的「vbDateTimeFormat」枚举,指定要对日期时间表达式应用的格式。若忽略,则将使用 vbGeneralDate」的值。

日期与时间格式 (vbDateTimeFormat 枚举)

已命名常量

数值

说明

vbGeneralDate

0

以系统的「通用日期」设置显示日期与/或时间。如果只有日期,则不显示时间;如果只有时间,则不显示日期。

vbLongDate

1

使用计算机区域设置指定的长日期格式显示日期。

vbShortDate

2

使用计算机区域设置指定的短日期格式显示日期。

vbLongTime

3

以系统「长时间」设置定义的格式显示时间。

vbShortTime

4

使用 24 小时格式显示时间 (hh:mm)。


错误代码:

13 数据类型不匹配

示例:


        REM  *****  BASIC  *****
        Option VBASupport 1
        Sub DateFormat
         Dim d as Date
         d = ("1958-01-29 00:25")
         msgbox("通用日期格式 : " & FormatDateTime(d))
         msgbox("长日期格式 : " & FormatDateTime(d,vbLongDate))
         msgbox("短日期格式 : " & FormatDateTime(d,vbShortDate))
         msgbox("长时间格式 : " & FormatDateTime(d,3))
         msgbox("短时间格式 : " & FormatDateTime(d,vbShortTime))
        End Sub
    

请支持我们!