DateAdd function malfunctions?

L

Larry Lard

Rich said:
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.
 
R

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.
 
R

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
 

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

Top