Wrox Programmer Forums
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 14th, 2004, 03:41 PM
Friend of Wrox
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default DateDiff Weirdness

I just discovered that DateDiff can give you a wrong answer if you were expecting what I was, so I wanted share it.

If you have Date1 = #12/31/2003# and Date2 = #1/1/2004#, then

Int((Date2 - Date1) / 365.25) = 0


DateDiff("yyyy", Date1, Date2) = 1

I thought this was a bug because clearly only one day has passed so the DateDiff should return zero also. But in the DateDiff help it says:
quote:When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.
I also noticed that counting months and weeks ("m" and "ww", respectively) are wrong, too.

This is terrible... DateDiff should return the true number of months and years elapsed, not just subtract the month or year parts from each other!

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division
Old July 16th, 2004, 12:38 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 308
Thanks: 0
Thanked 0 Times in 0 Posts

I came across a similar thing in December:

But I think it makes perfect sense now.
If you want to find the difference in years between two dates, then DateDiff("yyyy", Date1, Date2) is perfect.
If you want to do real calculations with Dates, then I'd just use Date1-Date2 and Multiply/Divide it by however you want it formatted.


I am a loud man with a very large hat. This means I am in charge

Similar Threads
Thread Thread Starter Forum Replies Last Post
Array weirdness? oldmainframehack Beginning PHP 1 September 5th, 2007 03:24 PM
Session variable weirdness LenexaKS ASP.NET 2.0 Professional 5 June 4th, 2007 11:05 AM
DropDownList.SelectedValue weirdness Aaron Edwards ASP.NET 2.0 Basics 5 March 7th, 2007 12:39 AM
GridView HeaderStyle Weirdness Bob Bedell ASP.NET 2.0 Basics 2 December 25th, 2006 02:28 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.