By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,694 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,694 IT Pros & Developers. It's quick & easy.

DateAdd function malfunctions?

P: n/a
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.
Feb 10 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

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

Feb 10 '06 #2

P: n/a
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

"Larry Lard" <la*******@hotmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.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

Feb 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.