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