DateAdd function malfunctions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rich Raffenetti

    DateAdd function malfunctions?

    I have the following code. If I do the dateadd function with
    dateinterval.mi nute, it works fine and the date/time value is displayed with
    zero seconds. If I do the dateadd function with dateinterval.se cond, 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 = &H1C5EA3B5F585F 1F
    Const dateRef As Date = #1/1/1601#
    TextBox1.Text = CStr(DateAdd(Da teInterval.Minu te, CDbl(largeInteg er
    / (60 * 10000000)), dateRef))
    'TextBox1.Text = CStr(DateAdd(Da teInterval.Seco nd, CDbl(largeInteg er
    / 10000000), dateRef))

    I am doing this in Visual Web Developer Express.


  • Larry Lard

    #2
    Re: DateAdd function malfunctions?


    Rich Raffenetti wrote:[color=blue]
    > I have the following code. If I do the dateadd function with
    > dateinterval.mi nute, it works fine and the date/time value is displayed with
    > zero seconds. If I do the dateadd function with dateinterval.se cond, 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 = &H1C5EA3B5F585F 1F
    > Const dateRef As Date = #1/1/1601#
    > TextBox1.Text = CStr(DateAdd(Da teInterval.Minu te, CDbl(largeInteg er
    > / (60 * 10000000)), dateRef))
    > 'TextBox1.Text = CStr(DateAdd(Da teInterval.Seco nd, CDbl(largeInteg er
    > / 10000000), dateRef))
    >
    > I am doing this in Visual Web Developer Express.[/color]

    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(DateIn terval.Second,2 147000000,now)
    #2/23/2074 3:49:47 AM#
    ?dateadd(DateIn terval.Second,2 148000000,now)
    overflow exception

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

    TextBox1.Text = dateRef.AddMinu tes(CDbl(largeI nteger / (60 *
    10000000))).ToS tring
    'or
    TextBox1.Text = dateRef.AddSeco nds(CDbl(largeI nteger /
    10000000)).ToSt ring
    '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

    Comment

    • Rich Raffenetti

      #3
      Re: DateAdd function malfunctions?

      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" <larrylard@hotm ail.com> wrote in message
      news:1139583364 .826212.247200@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      >
      > Rich Raffenetti wrote:[color=green]
      >> I have the following code. If I do the dateadd function with
      >> dateinterval.mi nute, it works fine and the date/time value is displayed
      >> with
      >> zero seconds. If I do the dateadd function with dateinterval.se cond, 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 = &H1C5EA3B5F585F 1F
      >> Const dateRef As Date = #1/1/1601#
      >> TextBox1.Text = CStr(DateAdd(Da teInterval.Minu te,
      >> CDbl(largeInteg er
      >> / (60 * 10000000)), dateRef))
      >> 'TextBox1.Text = CStr(DateAdd(Da teInterval.Seco nd,
      >> CDbl(largeInteg er
      >> / 10000000), dateRef))
      >>
      >> I am doing this in Visual Web Developer Express.[/color]
      >
      > 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(DateIn terval.Second,2 147000000,now)
      > #2/23/2074 3:49:47 AM#
      > ?dateadd(DateIn terval.Second,2 148000000,now)
      > overflow exception
      >
      > Workaround (well, fix): don't use the legacy VB functions; the
      > Framework's date handling is better:
      >
      > TextBox1.Text = dateRef.AddMinu tes(CDbl(largeI nteger / (60 *
      > 10000000))).ToS tring
      > 'or
      > TextBox1.Text = dateRef.AddSeco nds(CDbl(largeI nteger /
      > 10000000)).ToSt ring
      > '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
      >[/color]


      Comment

      Working...