Share via

DateDiff in Visual Basic 2013 returns wrong answers

Anonymous
2015-10-01T23:50:31+00:00

With time interval set at days, the difference in days between 2 dates is one less than the correct answer.

The exception is when the 2 dates are the same, where it returns 0 which is correct.

Examples of DateDiff giving wrong answers:

Using DateDiff, the difference in days between today and tomorrow is 0 (should be 1).

Using DateDiff, the difference in days between yesterday and tomorrow is 1 (should be 2).

The DateDiff function in SQL Server works correctly.  It should work correctly in VB 2013. 

When I registered this question in Microsoft Community, I couldn't find "Visual Basic" for a Category so I used "Office".  I couldn't find "Visual Basic" for an Office Topic so I used "Access".

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

ScottGem 68,830 Reputation points Volunteer Moderator
2015-10-02T12:04:21+00:00

First there is no such thing as Visual Basic 2013. If you are programming in VB, then you are using vb.Net. And you should be posting this under the MSDN forums. 

Office applications use VBA (Visual Basic for Applications). This is a superset of the VB language that adds commands, classes, objects that are specific to the application one is working with. So if your problem is with VBA (which isn't clear) then it would be appropriate to post under the application you are working with (Word, Access, Excel, etc.)

I just did the same thing Tom did, but I did it in Access, Excel and Word. In all instances it returned 1. 

So can you explain further?

Was this answer helpful?

0 comments No comments

Answer accepted by question author

Tom van Stiphout 40,211 Reputation points MVP Volunteer Moderator
2015-10-02T03:07:32+00:00

> Using DateDiff, the difference in days between today and tomorrow is 0 (should be 1).

Hit Ctrl+G to open the immediate window, and enter this:

?datediff("d", #10/1/2015#, #10/2/2015#)

What does it return?

1, which is what you wanted.

Given this example, can you discern what is different about your calculation?

Was this answer helpful?

0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Anonymous
    2015-10-02T15:18:03+00:00

    MSDN did provide an answer to me.  VS 2013 has some new language features that provide the ability to manipulate dates.  These are the TimeSpan methods.  DateDiff and other related date functions are not needed any longer.

    Thanks for your reply.

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,830 Reputation points Volunteer Moderator
    2015-10-02T14:21:21+00:00

    I had a feeling you were referring to VS 2013. Unfortunately I don't have that installed here to test. But I'm sure, if you posted in the VS area under MSDN, you'll get an answer.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2015-10-02T13:36:47+00:00

    I agree with you, My question does not belong in Microsoft Community.  Thanks for pointing that out.  I realized that myself, and recently I did re-post my question in MSDN.  Sorry about that, I should have realized it sooner.

    I am marking your reply as the Answer.

    I can explain the difference.  Visual Studio 2013 uses a different version of the DateDiff function than the version used by Office programs.

    Your tests in Office revealed no problem but that is because Office VB code is based on VB 6.0.  In VB 6.0, the version of DateDiff works fine, as your examples proved.

    That is, I wrote a program in VB 6.0 which works correctly, but when I rewrote the same program using Visual Studio 2013, it has the "off by 1" problem.

    Was this answer helpful?

    0 comments No comments