从DateDiff函数获取了错误的值 [英] Got Wrong Value From DateDiff function
问题描述
我正在将一个应用程序VB6迁移到VB.Net.
我在DateDiff函数中得到了错误的值.
我有一段代码需要修改:
在VB6代码中,
Dim n as Integer Dim CurDate as Date Dim FindDate AS Date CurDate = Now() FindDate = DateAdd("n", -540, CurDate) n = DateDiff("d", FindDate, CurDate)
==>这里将n值返回为1
在VB.Net代码中,
Dim CurDate as Date Dim FindDate AS Date CurDate = Now FindDate = CurDate.AddMinutes(-540) Dim dateDiff AS TimeSpan = FindDate.Subtract(CurDate) n = dateDiff.Days
==>这里将n值返回为0
我无法更改VB6代码.但是答案在VB.Net代码中应该相同,所以我要这样做.
感谢您的帮助!
您为什么希望减去9个小时会给您一天的差价?确定要在09:00之前对VB6和09:00之后对VB.NET代码进行测试吗?
实际上在VB6中,DateDiff函数考虑了日期但是在VB.Net中考虑了时间.
例如:
CurDate = "06/27/2011 4:01:15 AM" FindDate = "06/26/2011 7:01:15 PM" ''==> with subtraction of "-540" minutes
在VB中n = DateDiff("d", FindDate, CurDate)''==>它返回"1"值,因为我提到间隔为"d".所以只考虑日期.
在VB.Net中,Dim dateDiff AS TimeSpan = FindDate.Subtract(CurDate) n = dateDiff.Days''==>它返回"0"值,因为它会随着时间的推移而考虑.所以还没到24小时.
解决方案:
我更改了VB.Net代码,
n = DateDiff(DateInterval.Day, FindDate.Date, CurDate.Date)所以现在只考虑日期.
现在我得到"1"值
I am migrating one application VB6 to VB.Net.
I am getting wrong value in DateDiff function.
I have a piece of code which I need to modify:
in VB6 Code,
Dim n as Integer Dim CurDate as Date Dim FindDate AS Date CurDate = Now() FindDate = DateAdd("n", -540, CurDate) n = DateDiff("d", FindDate, CurDate)
==> here returns n value as 1
in VB.Net Code,
Dim CurDate as Date Dim FindDate AS Date CurDate = Now FindDate = CurDate.AddMinutes(-540) Dim dateDiff AS TimeSpan = FindDate.Subtract(CurDate) n = dateDiff.Days
==> here returns n value as 0
I can''t change VB6 code. But answer should be same in VB.Net code, so what i do for that.
Thanks for Any Help!解决方案Why are you expecting that subtracting nine hours will give you a day''s worth of difference? Are you sure that you just didn''t test at before 09:00 for the VB6 and after 09:00 for the VB.NET code?
Actually in VB6, DateDiff function considered date but in VB.Net considered with time.
for Example:
CurDate = "06/27/2011 4:01:15 AM" FindDate = "06/26/2011 7:01:15 PM" ''==> with subtraction of "-540" minutes
in VB,n = DateDiff("d", FindDate, CurDate)''==> it''s returned "1" value because i mentioned interval as "d". so it''s consider only date.
in VB.Net,Dim dateDiff AS TimeSpan = FindDate.Subtract(CurDate) n = dateDiff.Days''==> it''s returned "0" value because it''s consider with time. so it''s not reached 24 hours.
Solution:
i changed VB.Net Code,
n = DateDiff(DateInterval.Day, FindDate.Date, CurDate.Date)So now it''s consider only date.
Now i am getting "1" value
这篇关于从DateDiff函数获取了错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!