Results 1 to 4 of 4

Thread: Getting wrong date ??

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2015
    Posts
    17

    Getting wrong date ??

    I have striped this code to the parts needed to demonstrate my problem.

    Code:
    Dim MyStartDay As Date
    
    SELECT SchueckWkStart FROM DSWkNo where SchueckJobNo = '107 - 123456'
    
    MyStartDay = CDate(SchueckWkStart)
    This query returns a SchueckWkStart date as "2017-11-06", which is correct, but the "MyStartDay" variable will contain "2017-11-07", a day added immediately.

    Can anyone explain why?

  2. #2
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,668

    Re: Getting wrong date ??

    It is just a guess since we don't know what database you are using, but dates are usually stored as a double precision number with the date represented as an offset by the whole number since January 1, 1900, and the time is the decimal portion as the number of seconds since midnight. The error you are seeing is probably due to a round off as the double precision number is converted to a whole number.

    J.A. Coutts

  3. #3
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,734

    Re: Getting wrong date ??

    Good point couttsj.
    Probably adding an Int() will then fix it.

    MyDate = CDate(Int(WkStart))

  4. #4
    Frenzied Member
    Join Date
    Dec 2012
    Posts
    1,668

    Re: Getting wrong date ??

    I don't think an integer will handle it in this case, as anything over 32767 will be interpreted as a negative number.
    This is how a date is stored:
    Debug.Print CDbl(CDate("11/15/2017 8:42:32 PM"))
    43054.8628703704
    This is how it is interpreted as a long:
    Debug.Print CLng(CDate("11/15/2017 8:42:32 PM"))
    43055
    And this is what you get using Fix:
    Debug.Print CLng(Fix(CDate("11/15/2017 8:42:32 PM")))
    43054

    J.A. Coutts

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width