Wrox Programmer Forums
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access 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 3rd, 2008, 06:04 AM
Authorized User
Join Date: May 2006
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Exact year month and days

hi everybody,
Once again I am here to take your help. Please let me know how I will be able to get accurate days, months and years. I have two fields
1. Date of Joining [12-03-1990]dd/mm/yyyy
2. Date of retirement [03-07-2008]dd/mm/yyyy
in a query I want total days months and year the employee devoted in his services. [Date_of_joining]-[Date_of_retirement]. It should be 22 years 03 month and 18 days.

Age: DateDiff("yyyy",[Date_of_joining],[Date_of_retirement])but it gives only year.

thanks with regards

harish mankoti

Old July 3rd, 2008, 10:05 AM
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

Not quite... using DateDiff with the "yyyy" might give you a misleading answer because if you use it to find the difference between 1 January 2008 and 31 December 2007, you'll find that it says ONE YEAR when only ONE DAY has passed!

Fast and dirty with inaccuracies:

My advice is to use it to calculate total number of days ONLY (i.e. use "d", not "yyyy"). For convenience, I'll use all integers. Then you have:

intDays = DateDiff("d", [Date_of_Joining], [Date_of_Retirement])
intYears = CInt(intDays / 365.25)
intMonths = CInt(intDays / 30) - (intYears * 12)
intDays = intDays - CInt(intYears * 365.25) - (intMonths * 30)

This is approximate because there are either 365 or 366 days in the year with leap years. Also months are 28, 29, 30, or 31 days long.

More Accurate:

Loop from hire to retirement days day by day. Have three counters: intDays, intMonths, intYears. The counter starts with all = 0. As the counter goes, intDays adds 1. When it hits one month = DateSerial(Year([Date_of_Joining]), Month([Date_of_Joining]) + (intMonths + 1), Day([Date_of_Joining])) then add 1 to intMonths and reset intDays to zero. When it hits the anniversary date DateSerial(Year([Date_of_Joining]) + intYears + 1, Month([Date_of_Joining]), Day([Date_of_Joining])), then add 1 to intYears and reset intDays and intMonths to zero. Etc.

I'm sure there are other techniques out there.

Greg Serrano
Michigan Dept. of Environmental Quality, Air Quality Division

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using month & year variable to get date in range rsearing ASP.NET 2.0 Professional 2 July 5th, 2008 08:30 AM
Month and Year to Date on a Report Mitch Access 2 February 20th, 2007 10:23 AM
Days in a Month jmss66 Classic ASP Basics 3 April 11th, 2005 05:57 PM
get year,month,day,hour... kobystud C# 1 July 16th, 2004 04:45 PM
Month() Year() questions eon ASP.NET 1.0 and 1.1 Basics 1 October 4th, 2003 04:12 AM

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