Home All Groups Group Topic Archive Search About

DateAdd function malfunctions?

Author
10 Feb 2006 7:28 PM
Rich Raffenetti
I have the following code.  If I do the dateadd function with
dateinterval.minute, it works fine and the date/time value is displayed with
zero seconds.  If I do the dateadd function with dateinterval.second, an
error is thrown saying I have an overflow.  I would be happy to know if I am
doing something wrong or if I could do it differently to get the seconds to
display properly.

        Const largeInteger As Long = &H1C5EA3B5F585F1F
        Const dateRef As Date = #1/1/1601#
        TextBox1.Text = CStr(DateAdd(DateInterval.Minute, CDbl(largeInteger
/ (60 * 10000000)), dateRef))
        'TextBox1.Text = CStr(DateAdd(DateInterval.Second, CDbl(largeInteger
/ 10000000), dateRef))

I am doing this in Visual Web Developer Express.

Author
10 Feb 2006 2:56 PM
Larry Lard
Rich Raffenetti wrote:
Show quoteHide quote
> I have the following code.  If I do the dateadd function with
> dateinterval.minute, it works fine and the date/time value is displayed with
> zero seconds.  If I do the dateadd function with dateinterval.second, an
> error is thrown saying I have an overflow.  I would be happy to know if I am
> doing something wrong or if I could do it differently to get the seconds to
> display properly.
>
>         Const largeInteger As Long = &H1C5EA3B5F585F1F
>         Const dateRef As Date = #1/1/1601#
>         TextBox1.Text = CStr(DateAdd(DateInterval.Minute, CDbl(largeInteger
> / (60 * 10000000)), dateRef))
>         'TextBox1.Text = CStr(DateAdd(DateInterval.Second, CDbl(largeInteger
> / 10000000), dateRef))
>
> I am doing this in Visual Web Developer Express.

I'd say this counts as a bug. The DateAdd function, despite asking for
a Double as its second argument, seems to be converting that double to
an Int32 at some point. Evidence:

?dateadd(DateInterval.Second,2147000000,now)
#2/23/2074 3:49:47 AM#
?dateadd(DateInterval.Second,2148000000,now)
overflow exception

Workaround (well, fix): don't use the legacy VB functions; the
Framework's date handling is better:

        TextBox1.Text = dateRef.AddMinutes(CDbl(largeInteger / (60 *
10000000))).ToString
        'or
        TextBox1.Text = dateRef.AddSeconds(CDbl(largeInteger /
10000000)).ToString
        'both work fine

Even better (in terms of self-documenting code), convert largeInteger
to a TimeSpan (documenting the conversion factor), then just add it to
dateref.


--
Larry Lard
Replies to group please
Author
11 Feb 2006 4:42 AM
Rich Raffenetti
Larry,
    Thanks a load.  It works great!  I was hoping for a different way like
this.  It's not always clear what is legacy.

    Do you recommend any reference books that describe these nitty-gritty
details for .Net?
Rich

Show quoteHide quote
"Larry Lard" <larryl***@hotmail.com> wrote in message
news:1139583364.826212.247200@g14g2000cwa.googlegroups.com...
>
> Rich Raffenetti wrote:
>> I have the following code.  If I do the dateadd function with
>> dateinterval.minute, it works fine and the date/time value is displayed
>> with
>> zero seconds.  If I do the dateadd function with dateinterval.second, an
>> error is thrown saying I have an overflow.  I would be happy to know if I
>> am
>> doing something wrong or if I could do it differently to get the seconds
>> to
>> display properly.
>>
>>         Const largeInteger As Long = &H1C5EA3B5F585F1F
>>         Const dateRef As Date = #1/1/1601#
>>         TextBox1.Text = CStr(DateAdd(DateInterval.Minute,
>> CDbl(largeInteger
>> / (60 * 10000000)), dateRef))
>>         'TextBox1.Text = CStr(DateAdd(DateInterval.Second,
>> CDbl(largeInteger
>> / 10000000), dateRef))
>>
>> I am doing this in Visual Web Developer Express.
>
> I'd say this counts as a bug. The DateAdd function, despite asking for
> a Double as its second argument, seems to be converting that double to
> an Int32 at some point. Evidence:
>
> ?dateadd(DateInterval.Second,2147000000,now)
> #2/23/2074 3:49:47 AM#
> ?dateadd(DateInterval.Second,2148000000,now)
> overflow exception
>
> Workaround (well, fix): don't use the legacy VB functions; the
> Framework's date handling is better:
>
>        TextBox1.Text = dateRef.AddMinutes(CDbl(largeInteger / (60 *
> 10000000))).ToString
>        'or
>        TextBox1.Text = dateRef.AddSeconds(CDbl(largeInteger /
> 10000000)).ToString
>        'both work fine
>
> Even better (in terms of self-documenting code), convert largeInteger
> to a TimeSpan (documenting the conversion factor), then just add it to
> dateref.
>
>
> --
> Larry Lard
> Replies to group please
>