- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 IsDate("13.50")
返回 True
而 IsDate("12.25.2010")
返回 False
?
最佳答案
我最近被这个小“功能”绊倒了,希望提高人们对 VB 和 VBA 中 IsDate
函数的一些问题的认识。
正如您所期望的,IsDate
在传递日期数据类型时返回 True
,对于除字符串之外的所有其他数据类型返回 False
。对于字符串,IsDate
根据字符串的内容返回 True
或 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
正如 @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/
为什么 IsDate("13.50") 返回 True 而 IsDate("12.25.2010") 返回 False? 最佳答案 我最近被这个小“功能”绊倒了,希望提高人们对 VB 和 VBA 中
刚注意到isDate不适用于 arrayformula . 案件 如果日期要过滤所有值: 使用公式: =FILTER(data,ISDATE(data)) 预期结果: 8/28/2018 得到了: #
这个问题已经有答案了: What is the significance of 1/1/1753 in SQL Server? (6 个回答) 已关闭 6 年前。 MS 文档指出 ISDATE() R
我有一张表,需要验证某个列是否只包含日期。我正在尝试计算不遵循日期格式的记录数。如果我检查一个未定义为“日期”类型的字段,则查询有效。但是,当我检查我定义为日期的字段时,它没有。 询问: SELECT
我有这样的日期字符串 2010 年 5 月 1 日到 2016 年 8 月 15 日。我只想在我的函数中将其作为日期字符串进行检查,而不是继续执行函数。当我检查这个时带有 new Date(value
当用户在文本框中输入日期 05/04 并按 Enter 时,我使用以下代码来检查它是否是日期。代码是 window.execScript("dim successReturn:successRetu
我有一行 SQL Server 代码,它采用列为“YYYYMMDD”的日期,其中 DD 为 00,并将 00 转换为 01,以便它与日期时间一起使用。我希望能够为此使用 MySQL 适用于 SQL 服
IsDate 函数 返回 Boolean 值指明某表达式是否可以转换为日期。 IsDate(expression) expression 参数可以是任意可被识别为日期和时间的日期表达式或字符串
DB2 版本为 9.7.0.7 我有一个平面文件,需要在插入生产表之前进行充分验证。为了分析,我将其解析为一个表,其中所有列都是 VARCHAR . 其中一项任务是验证日期。我需要能够找到具体的无效日
我正在针对从 Excel 文件导入的数据运行 SQL 语句。在此 SQL 中,我使用 IsDate 函数检查用户是否正确输入了日期。由于这是尚未转换的原始数据,因此所有日期都存储在 varchar 数
我正在尝试检查一个值是否是这样的日期: var input = "2014-04-01T15:22:30.000Z"; var dateTime = { year: $filter('date
这个问题在这里已经有了答案: Unable to convert MySQL date/time value to System.DateTime.Couldn't store 0/0/0000 0
我对 Calendar.current.isDate 这个函数有疑问。 let calendar = Calendar.current print("locale : \(calendar.loc
如果之前有人问过这个问题,我深表歉意。我有一些数据需要存储为字符串,其中一些数据是日期。数据以字符串形式开始,例如“01/02/10”(英国格式)。现在,稍后会解析此数据,并且根据解析的内容,结果会有
在加载到目标数据库之前,有什么方法可以在从源文件提取后检查 SSIS 包派生列表达式中的日期(如 TSQL 中的 isDate 函数)列吗? 谢谢 最佳答案 没有内置函数,但您可以运行脚本任务并使用
在加载到目标数据库之前,有什么方法可以在从源文件提取后检查 SSIS 包派生列表达式中的日期(如 TSQL 中的 isDate 函数)列吗? 谢谢 最佳答案 没有内置函数,但您可以运行脚本任务并使用
我想将所有非日期格式的字符串更改为日期 nil,然后更改列类型。我曾计划使用以下内容: class SetSubmitDateForAllMyTables select '1/2/2015'::da
我知道有一个函数叫 ISDATE验证 DATETIME列,但它仅适用于 SMALLDATETIME和 DATETIME类型。 是否有类似的方法来验证新的数据类型 DATETIME2在 SQL Serv
Declare @var varchar(100) set @var = '2000' select case when ISDATE(@var) = 1 then '
因此,我查看了 SQL Server 中 ISDATE() 函数的文档,并在示例中看到了这一点: SET DATEFORMAT mdy; SELECT ISDATE('15/04/2008'); --
我是一名优秀的程序员,十分优秀!