473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DateDiff

147 100+
How do I get datediff to give me an accurate count of years. if I enter a date of November 2, 2008 it shows me 1 year as of Today instead of zero. How do I make it count on the actual anniversary?????

Please help
Jan 20 '09 #1
6 2142
puppydogbuddy
1,923 Expert 1GB
Anniversary: DateDiff("yyyy", [AnniversaryDate], Now())+ Int( Format(Now(), "mmdd") < Format( [AnniversaryDate], "mmdd") )
__________________________________________________ __________
Here is how it works:

Anniversary will be the number of years from the DateDiff calculation plus 0, if we have passed the anniversary day already, or plus negative 1 (-1), if the anniversary day is still in the future.

DateDiff("yyyy", [AnniversaryDate], Now()) calculates the difference between the year of the anniversary date and the current year. This is all you would need if you KNEW that the anniversary day for this year had already passed.

Format(Now(), "mmdd") returns the month and day for today's date.

Format( [AnniversaryDate], "mmdd") returns the month and day for the anniversary date

Format(Now(), "mmdd") < Format( [AnniversaryDate], "mmdd") asks if the month and day for today is before the month and day in the anniversary date.

Int(expression) The Int function returns the integer part of a number. In this case we are using the Int function to get a number (Boolean 0 = False , -1 = True) out of a logical comparison.

Example: if today is January 20, 2009 and the anniversary date is May 25, 1972, we are looking at the question "Is January 20 < May 25", which is True.
So when today's date is before the anniversary date, the Int part is -1 and the full expression for Anniversary adds -1 to the DateDiff. This gives the correct anniversary, as of today. As soon as Now() gets to May 25, the Int part of the expression evaluates to 0 and the DateDiff result is the correct anniversary on that date.
Jan 20 '09 #2
DAHMB
147 100+
Thank You!!! I had been bangin my head on this!!
Jan 21 '09 #3
missinglinq
3,532 Expert 2GB
PDB's solution, with its excellent explanation, is the standard way of determining a person's actual age at any giving point in time.. In general, there's two things you have to keep in mind when using the DateDiff() function:

  1. DateDiff() always returns the difference of the interval you specified
  2. How accurate a date difference do you need?
As you found out, if you specify "yyyy" as the Interval argument, Access will return the difference in the year portion of the two dates. If you use year as your Interval, then 12/31/2008 and 1/1/2009 will return a difference in years of 1, even though, in actuality, it's only a difference of 1 in days!. In other words, Access gives you what asked for!

As I said, you also have to think about the accuracy you need. Using the dates

11/1/2008
and 12/31/2008

and calculating the difference in months

will yield 1, the difference, in months, between 11/2008 and 12/2008.

Using the same dates but calculating the difference in days

will yield 60, which most people would consider to be 2 months, not 1 month!

To work around this, you need to use DateDiff() with the lowest common denominator, if you will, that will give you the accuracy you need, then divide it by your definition of the unit of date or time. So if you define a month as 30 days, to determine the difference, in months, between a StartDate of 11/1/2008 and an EndDate of 12/31/2008 you would do this:

Months = DateDiff("d", StartDate, EndDate)/30

which will yield 2 months.
.
Linq ;0)>
Jan 21 '09 #4
DAHMB
147 100+
Thanks Ling. I really appreciate when people take the time to teach, which is why I am here.
Dan
Jan 21 '09 #5
missinglinq
3,532 Expert 2GB
You're quite welcome, Dan! It's why we're here, too!

Linq ;0)>
Jan 22 '09 #6
NeoPa
32,556 Expert Mod 16PB
RATS!!!

I was sure it (DateDiff) returned the number of full years based on the full date :(

I shall have to remember that one.
Jan 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: CJM | last post by:
I have an ASP page that lists files and folders in a directory. I'm using a cookie to record the last time this page was visited, and I intend to show links that are created/modified from that date...
8
by: inamori | last post by:
I face that problems 07/01/2003 06/30/2006 ---------> it should be 3 01/01/2003 02/28/2005 --------->could i get 2 years and 2 months 01/01/2003 03/01/2005 ...
6
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
5
by: sr | last post by:
Anyone know of a better way to simulate a datediff for C#, i.e., without referencing the VB.NET runtime? Only added the functionality that was needed for me so it is not the full implementation...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.