gpt4 book ai didi

vba - IsDate 函数返回意外结果

转载 作者:行者123 更新时间:2023-12-02 03:41:29 24 4
gpt4 key购买 nike

为什么 IsDate("13.50") 返回 TrueIsDate("12.25.2010") 返回 False?

最佳答案

我最近被这个小“功能”绊倒了,希望提高人们对 VB 和 VBA 中 IsDate 函数的一些问题的认识。

简单案例

正如您所期望的,IsDate 在传递日期数据类型时返回 True,对于除字符串之外的所有其他数据类型返回 False。对于字符串,IsDate 根据字符串的内容返回 TrueFalse:

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

如果数字作为字符串传递,则尾随前导零很重要:

?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() 来转换它们。

关于vba - IsDate 函数返回意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com