Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 3rd, 2008, 06:04 AM
Authorized User
 
Join Date: May 2006
Location: delhi, delhi, India.
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

Reply With Quote
  #2 (permalink)  
Old July 3rd, 2008, 10:05 AM
Friend of Wrox
Points: 4,007, Level: 26
Points: 4,007, Level: 26 Points: 4,007, Level: 26 Points: 4,007, Level: 26
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Lansing, Michigan, USA.
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

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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 07:14 PM.


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