473,569 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calculate number years months days between 2 dates

1 New Member
I had read a previous article on calculating the number of years, months and days between 2 dates. They had used the DateDiff with a start date and an end date, similiar like below statement:

DateDiff("d",Da teAdd("m",[Months],DateAdd("yyyy" ,[Years],[DateHired])),[DateTerminated]) AS Days

When I have dates like 2011-05-09 for the DateHired and 2011-09-08 for DateTerminated the days come out as a -1.
Here is another example: For the DateHired 2009-06-30 and the DateTerminated 2011-06-15 the days come out as -15.
One more example: DateHired 2008-09-29 and DateTerminated 2011-09-23 the days come out as -6.

I am able to get the years and months correctly but the some of the days end up being negative and incorrect.
Dec 28 '11 #1
2 3571
TheSmileyCoder
2,322 Recognized Expert Moderator Top Contributor
Am I correct in understand that you need each value calculated seperately? It is not enough for you to simply have the value in days? Is that the issue?
Dec 28 '11 #2
NeoPa
32,564 Recognized Expert Moderator MVP
Just as an introduction, this will probably work most reliably using the Year(), Month() & Day() function values for each date. It seems, from your question, that you are trying to work in SQL. A good idea to mention that generally, but I think we can assume from what you let slip.

The following should get you somewhere quite close :
Expand|Select|Wrap|Line Numbers
  1. SELECT Year([DateTerminated]) - Year([DateHired]) -
  2.        IIf(Month([DateTerminated]) = Month([DateHired]),
  3.        IIf(Day([DateTerminated]) < Day([DateHired]), 1,
  4.        IIf(Month([DateTerminated]) < Month([DateHired]), 1, 0))) AS [Years]
  5.      , (12 + Month([DateTerminated]) - Month([DateHired]) -
  6.        IIf(Day([DateTerminated]) < Day([DateHired]), 1, 0)) Mod 12 AS [Months]
  7.      , Day(DateAdd('d', -Day([DateHired]), DateAdd('m', 1, [DateHired]))) AS [DIM]
  8.      , ([DIM] + Day([DateTerminated]) - Day([DateHired])) Mod [DIM] AS [Days]
Let us know how well that works for you.
Dec 28 '11 #3

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

Similar topics

7
12806
by: Bambero | last post by:
Hello all Problem like in subject. There is no problem when I want to count days between two dates. Problem is when I want to count years becouse of leap years. For ex. between 2002-11-19 2003-11-19
3
3343
by: MMFBprez | last post by:
I am trying to compute storage charges by getting the number of months between dates and multiplying it by a rate. I cannot get a correct number of months if the date is greater than a year ago. Here is the formula I am using now to get the number of months: #ofMonths: Month(.-.)-1. It has worked for me until now because it never was...
6
27952
by: carl.barrett | last post by:
Hi, I have a continuous form based on a query ( I will also be creating a report based on the same query). There are 2 fields: Date Obtained and Date Of Expiry I want a further 3 columns to the right of these 2 fields to show the
5
8812
by: Juan | last post by:
Hi everyone, is there a function that calculates the exact amount of Years, Months, and Days between two days? TimeDiff is not good enough, since it calculates, for example: Date 1: Dec. 31st 2003 Date 2: Jan 1st 2004
8
1760
by: Jose | last post by:
Exists a function that determined between two dates the years,months and days? Thanks a lot.
23
14009
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that didn't work (the timedelta class doesn't have a year accessor). I looked in the docs and the cookbook, but I couldn't find anything, so
15
42604
karthickkuchanur
by: karthickkuchanur | last post by:
Hai sir, i assigned to calculate the age from current date ,i have the date of birth of employee from that i have to calculate the age dynamically, i refer to google it was confusing me .please give some idea to do sir
2
4005
by: johanneguaybenoit | last post by:
Hi I would like to know how to create a function or module to calculate the age of a person in years months days. and hours if feasable But I really nead to know in years months days. I am waiting a reply from you and many thanks for your help in advance. Johanne
4
11196
by: shilpareddy2787 | last post by:
Hello, I have some total values, I want to calculate percenatge of these Total Values. I want to divide the total with No. Of working Days Excluding Saturdays and Sundays in a given period. How to calculate the Total Number of working Days in a given period . Let us say If i give the period as 08/01/2008 to 08/15/2008, I want total number...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8132
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7678
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6286
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5222
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3656
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3644
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1226
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.