Click here to Skip to main content
15,510,802 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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?
Share this answer
Ramanujam Shankar 27-Jun-11 8:26am    
i am getting "-540" value from one variable. without subtract then it will working fine. but in vb code was written like subtract "-540" value. then find the difference
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.

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
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900