Difference between two dates in Visual Basic

Posted on 2012-09-17
Last Modified: 2012-09-17
Trying to calculate the difference between two dates. I have tried to use DateDiff like follows: DateDiff("d", Now, StDate) where StDate = 9/22/2012. When I execute this in the immediate window[?SetWeek2(9/22/2012)] I get -41169. I also output the value of StDate. It is 12:00:18 AM. Apparently StDate is not being evaluated as 9/22/2012. I am sure this is something elementary. Thanks in advance for the help.
Question by:StampIT
    LVL 19

    Accepted Solution

    I just ran a quick test in Access...:

    Sub test()
        Dim x As Variant
        x = DateDiff("d", Now, #9/22/2012#)
    End Sub

    Open in new window

    It assigned a value of 5 to x.

    you have setweek(9/22/2012).... maybe you need to surround by # to indicate a date?
    LVL 17

    Expert Comment

    In my example I force stNow to be the same format as the date stDate that I am trying to compare

    Dim stDate As Date
    Dim stNow As Date

        stDate = "9 / 22 / 2012"
        stNow = Format(Now(), "mm/dd/yy")
    MsgBox (DateDiff("d", stNow, stDate))
    LVL 29

    Expert Comment

    In calling your function SetWeek2(9/22/2012), you are not sending a date value but rather the quotient of 9 divided by 22 divided by 2012.  In order for Access to see this as a date it needs to be enclosed with # symbols just as buttersk mentioned.
    LVL 48

    Expert Comment

    by:Gustav Brock
    Don't mix up the format with the value.
    It should read:

    Dim datDate As Date
    Dim datNow As Date
        datDate = #9/22/2012#
        datNow = Date


    Author Closing Comment

    It was as simple as that. Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
    I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
    Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now