2015-04-01 76 views
0

例输入:当我使用我不能使用DATEDIFF VBA

ans = datediff("s", date1, date2) 

它产生错误

date1 = "2015-03-23 07:06:17.855000" 
date2 = "2015-03-23 07:06:17.870000" 

type mismatch

我该如何解决这个问题?

回答

0

看起来你还没有成功的答案,所以这里有一个可能性。

Dim t() as string 
Dim d1 as long 
Dim d2 as long 

date1 = "2015-03-23 07:06:17.855000" 
date2 = "2015-03-23 07:06:17.870000" 
t = split(date1, ".") 'use the "." to split off the miliseconds 
d1 = clng(t(2))   'grab the milliseconds, convert it to long 
t = split(date2, ".") 'use the "." to split off the miliseconds from the other date 
d2 = clng(t(2))   'grab the milliseconds, convert it to long 

msgbox "Difference in milliseconds: " & cstr(d2-d1) 
0

尝试通过使用CDate("string_date")转换作为字符串存储到日期数据类型的日期:

Dim date1 As String, date2 As String, ans As Long 

date1 = "2015-03-23 07:06:17.855000" 
date2 = "2015-03-23 07:06:17.870000" 
ans = datediff("s", CDate(Left(date1, Len(date1)-7)), CDate(Left(date2, Len(date2)-7))) 
'returns 0, because both date and parts are the same! 

DateDiff功能的最小单位是一秒钟。

+0

是的,我试过,但它不工作 – Aoon 2015-04-01 09:26:04

+0

我现在看到它。 DateDiff函数可以以秒为单位返回时间差,但真正的差异以毫秒为单位。 'DateDiff'函数无法比较它,因为最小的单位是秒。当你通过日期参数这种方式:'CDate(Left(date1,Len(date1)-7)'它会执行没有错误 – 2015-04-01 09:45:10

+0

@Maciej洛杉矶可能要很长,并且你的括号是超出计数, “(”和只有6-“)” – Davesexcel 2015-04-01 10:38:22