DateDiff

R

rn5a

I have 2 variables - 'dt1' & 'dt2' - of type DateTime. Assume that one
of the values of 'dt1' (which is being retrieved from a DB table) is
27/12/2006 6:54:27 AM & the value of 'dt2' is the current date & time,
say, 30/12/2006 3:09:22 PM.

I have the following code which uses the built-in DateDiff function:

Dim dt1 As DateTime
Dim dt2 As DateTime

dt2 = DateTime.Now

If (DateDiff(DateInterval.Day, CDate(dt1), CDate(dt2)) < 3) Then
Label1.Text = "Less than 3 days have elapsed"
Else
Label1.Text = "3 or more than 3 days have elapsed"
End If

The DateDiff function above evaluates to 3 & since 3 < 3 evaluates to
False; the Else condition must get satisfied but when the value of
'dt1' is 27/12/2006 6:54:27 AM, then the If condition gets satisfied!

Why so?
 
J

Jim in Arizona

I have 2 variables - 'dt1' & 'dt2' - of type DateTime. Assume that one
of the values of 'dt1' (which is being retrieved from a DB table) is
27/12/2006 6:54:27 AM & the value of 'dt2' is the current date & time,
say, 30/12/2006 3:09:22 PM.

I have the following code which uses the built-in DateDiff function:

Dim dt1 As DateTime
Dim dt2 As DateTime

dt2 = DateTime.Now

If (DateDiff(DateInterval.Day, CDate(dt1), CDate(dt2)) < 3) Then
Label1.Text = "Less than 3 days have elapsed"
Else
Label1.Text = "3 or more than 3 days have elapsed"
End If

The DateDiff function above evaluates to 3 & since 3 < 3 evaluates to
False; the Else condition must get satisfied but when the value of
'dt1' is 27/12/2006 6:54:27 AM, then the If condition gets satisfied!

Why so?

You're right. I used pretty much the same code:

Dim dt1 As DateTime = CDate("12/27/2006 6:54:27 AM")
Dim dt2 As DateTime = CDate("12/30/2006 3:09:22 PM")

dt2 = DateTime.Now

If (DateDiff(DateInterval.Day, CDate(dt1), CDate(dt2)) < 3) Then
lbl1.Text = "Less than 3 days have elapsed"
Else
lbl1.Text = "3 or more than 3 days have elapsed"
End If

I also get the same, odd result. I am currently building a web app that
uses the datediff method and now I'm a bit speculative as to if my app
will work properly. I'll use DateInterval.Hour instead for my purposes.

As to why this happens, I don't know. I hope someone else on this board
does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top