Home All Groups Group Topic Archive Search About

DateDiff(DateInterval.DayOfYear

Author
5 Feb 2006 8:10 PM
Price Brattin
Why is the DateDiff function in the following code returning zero?

Dim FileDate, TransmissionDate as Date
Dim TranDay, FileDay, DayDiff as Inteter
TransmissionDate = #2/5/2006 1:57:56 PM#
FileDate = #2/4/2006 4:49:02 PM#
TranDay = DatePart(DateInterval.DayOfYear, TransmissionDate)    'Returns 36
FileDay = DatePart(DateInterval.DayOfYear, FileDate)  'Returns 35
DayDiff = DateDiff(DateInterval.DayOfYear, FileDate, TransmissionDate) 
'Returns 0



--
Price Brattin
Certified MBS/Solomon Consultant
SQLServer MCP
VB.Net Developer

Author
5 Feb 2006 8:19 PM
Cor Ligthert [MVP]
Price,

I did not do it, however can you change your sample in
Dim TranDay, FileDay, DayDiff as Double and than try it again.

Most calculations inside Net are done with doubles.

Cor
Author
5 Feb 2006 8:32 PM
Price Brattin
I will give it a try but I am puzzled what it would mean.  DatePart returns
an integer and DateDiff returns a long.  Declaring those fields as Double
would mean converting an integer to a Double.  The purpose of showing the
values returned was to show that DatePart does recognize the dates as
different days but DateDiff does not.  Why is that so?

--
Price Brattin
Certified MBS/Solomon Consultant
SQLServer MCP
VB.Net Developer



Show quoteHide quote
"Cor Ligthert [MVP]" wrote:

> Price,
>
> I did not do it, however can you change your sample in
> Dim TranDay, FileDay, DayDiff as Double and than try it again.
>
> Most calculations inside Net are done with doubles.
>
> Cor
>
>
>
Author
6 Feb 2006 8:09 AM
R. MacDonald
Hello, Price,

It appears that DateDiff returns only the number of complete time
intervals elapsed.  In effect it is truncating the difference to the
next smaller integer, rather than rounding it as you might expect.  So
for example, the difference is still zero (full) days even if the
Transmission date is #2/5/2006 4:49:01 PM#.  But add just one more
second, and the difference will become one (full) day.

Note that this behaviour of DateDiff is not consistent.  (And this is
mentioned/implied in the documentation.)  In the case of
DateInterval.Year, the non-year portion of the date is disregarded, so
that (for example):

     DateDiff(DateInterval.Year, _
              #12/31/2005 11:59:59 PM#,  _
              #1/1/2006 12:00:01 AM#)

returns a value of 1!  DateInterval.Month appears to work the same way
as DateInterval.Year.  (You've gotta love the programmer that thought
implementing DateDiff with this inconsistency would be a good idea.  I
guess that he got overwhelmed with the concepts involved in
DateInterval.Weekday and DateInterval.WeekOfYear. :(  )

Cheers,
Randy


Price Brattin wrote:
Show quoteHide quote
> Why is the DateDiff function in the following code returning zero?
>
> Dim FileDate, TransmissionDate as Date
> Dim TranDay, FileDay, DayDiff as Inteter
> TransmissionDate = #2/5/2006 1:57:56 PM#
> FileDate = #2/4/2006 4:49:02 PM#
> TranDay = DatePart(DateInterval.DayOfYear, TransmissionDate)    'Returns 36
> FileDay = DatePart(DateInterval.DayOfYear, FileDate)  'Returns 35
> DayDiff = DateDiff(DateInterval.DayOfYear, FileDate, TransmissionDate) 
> 'Returns 0
>
>
>