我最近被这个小“功能”绊倒了,想提高人们对围绕该功能的一些问题的认识。IsDate
VB 和 VBA 中的函数。
简单的案例
正如你所期望的那样,IsDate
回报True
当传递日期数据类型时False
对于除字符串之外的所有其他数据类型。对于字符串,IsDate
回报True
or False
基于字符串的内容:
IsDate(CDate("1/1/1980")) --> True
IsDate(#12/31/2000#) --> True
IsDate(12/24) --> False '12/24 evaluates to a Double: 0.5'
IsDate("Foo") --> False
IsDate("12/24") --> True
是日期时间吗?
IsDate
应该更准确地命名IsDateTime
因为它返回True
对于格式化为时间的字符串:
IsDate("10:55 AM") --> True
IsDate("23:30") --> True 'CDate("23:30") --> 11:30:00 PM'
IsDate("1:30:59") --> True 'CDate("1:30:59") --> 1:30:59 AM'
IsDate("13:55 AM") --> True 'CDate("13:55 AM")--> 1:55:00 PM'
IsDate("13:55 PM") --> True 'CDate("13:55 PM")--> 1:55:00 PM'
请注意上面最后两个示例IsDate
不是一个完美的验证器次。
问题!
不仅IsDate
接受时间,它接受多种格式的时间。其中之一使用句点 (.
) 作为分隔符。这会导致一些混乱,因为句点可以用作时间分隔符,但不能用作日期分隔符:
IsDate("13.50") --> True 'CDate("13.50") --> 1:50:00 PM'
IsDate("12.25") --> True 'CDate("12.25") --> 12:25:00 PM'
IsDate("12.25.10") --> True 'CDate("12.25.10") --> 12:25:10 PM'
IsDate("12.25.2010")--> False '2010 > 59 (number of seconds in a minute - 1)'
IsDate("24.12") --> False '24 > 23 (number of hours in a day - 1)'
IsDate("0.12") --> True 'CDate("0.12") --> 12:12:00 AM
如果您正在解析字符串并根据其表观类型对其进行操作,这可能会出现问题。例如:
Function Bar(Var As Variant)
If IsDate(Var) Then
Bar = "This is a date"
ElseIf IsNumeric(Var) Then
Bar = "This is numeric"
Else
Bar = "This is something else"
End If
End Function
?Bar("12.75") --> This is numeric
?Bar("12.50") --> This is a date
解决方法
如果您正在测试其基础数据类型的变体,您应该使用TypeName(Var) = "Date"
而不是IsDate(Var)
:
TypeName(#12/25/2010#) --> Date
TypeName("12/25/2010") --> String
Function Bar(Var As Variant)
Select Case TypeName(Var)
Case "Date"
Bar = "This is a date type"
Case "Long", "Double", "Single", "Integer", "Currency", "Decimal", "Byte"
Bar = "This is a numeric type"
Case "String"
Bar = "This is a string type"
Case "Boolean"
Bar = "This is a boolean type"
Case Else
Bar = "This is some other type"
End Select
End Function
?Bar("12.25") --> This is a string type
?Bar(#12/25#) --> This is a date type
?Bar(12.25) --> This is a numeric type
但是,如果您正在处理可能是日期或数字的字符串(例如,解析文本文件),则应该在检查它是否是日期之前检查它是否是数字:
Function Bar(Var As Variant)
If IsNumeric(Var) Then
Bar = "This is numeric"
ElseIf IsDate(Var) Then
Bar = "This is a date"
Else
Bar = "This is something else"
End If
End Function
?Bar("12.75") --> This is numeric
?Bar("12.50") --> This is numeric
?Bar("12:50") --> This is a date
即使您只关心它是否是日期,您也应该确保它不是数字:
Function Bar(Var As Variant)
If IsDate(Var) And Not IsNumeric(Var) Then
Bar = "This is a date"
Else
Bar = "This is something else"
End If
End Function
?Bar("12:50") --> This is a date
?Bar("12.50") --> This is something else
CDate 的特点
正如@Deanna 在下面的评论中指出的那样,CDate()
也是不可靠的。其结果根据传递的是字符串还是数字而有所不同:
?CDate(0.5) --> 12:00:00 PM
?CDate("0.5") --> 12:05:00 AM
尾随and如果数字作为字符串传递,则前导零很重要:
?CDate(".5") --> 12:00:00 PM
?CDate("0.5") --> 12:05:00 AM
?CDate("0.50") --> 12:50:00 AM
?CDate("0.500") --> 12:00:00 PM
当字符串的小数部分接近 60 分钟标记时,行为也会发生变化:
?CDate("0.59") --> 12:59:00 AM
?CDate("0.60") --> 2:24:00 PM
最重要的是,如果您需要将字符串转换为日期/时间,您需要了解您期望它们采用的格式,然后在依赖之前适当地重新格式化它们CDate()
来转换它们。