473,699 Members | 2,380 Online

# Calculate Number of Years, Months and Days between 2 dates

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
number of Years, Months and Days between the 2 given dates.

In the query I have the dates as the first 2 columns. In the third
column I have:

Column 3 Has - Years: DateDiff("yyyy" ,[DateObtained],[DateOfExpiry])
Column 4 Has - Months: DateDiff("m",[DateObtained],[DateOfExpiry])
Column 5 Has - Days:

However my data is not coming out quite how I would like it, for
example

Date Obtained Date Of Expiry Years Months Days
14/06/05 14/06/06 1 12 0
30/06/05 03/08/05 1 2 -27

On the first example I just want it to show: 1 0 0
On the second I want it to show: 0 1 3

I hope this makes sense and hope someone can help me out here.

Please make any recommended solutions as clear as possible.

Thanks in advance to anyone who takes the time to help me out here.

Regards.

Nov 13 '05 #1
6 27968
Carl:

Below is a query that, from my limited testing at least, appears to give you
the information you are looking for. Your second example says the result
should be 0 1 3, however, my calculations return 0 1 4, given that July is
31 days, plus the first three days of August. I had to adjust the month
calculation for the obtained Day being less than or equal the Expired Day as
the DateDiff function just subtracts Months.

SELECT TestDate.DateOb tained, TestDate.DateEx pired,
DateDiff("yyyy" ,[DateObtained],[DateExpired]) AS Years,
IIf(Day([DateObtained])<=Day([DateExpired]),DateDiff("m",[DateObtained],[DateExpired])-[Years]*12,DateDiff("m ",[DateObtained],[DateExpired])-[Years]*12-1)
AS Months,
AS Days
FROM TestDate;

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.
<ca**********@n ewcastle.gov.uk > wrote in message
news:11******** *************@g 43g2000cwa.goog legroups.com...
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
number of Years, Months and Days between the 2 given dates.

In the query I have the dates as the first 2 columns. In the third
column I have:

Column 3 Has - Years: DateDiff("yyyy" ,[DateObtained],[DateOfExpiry])
Column 4 Has - Months: DateDiff("m",[DateObtained],[DateOfExpiry])
Column 5 Has - Days:

However my data is not coming out quite how I would like it, for
example

Date Obtained Date Of Expiry Years Months Days
14/06/05 14/06/06 1 12 0
30/06/05 03/08/05 1 2 -27

On the first example I just want it to show: 1 0 0
On the second I want it to show: 0 1 3

I hope this makes sense and hope someone can help me out here.

Please make any recommended solutions as clear as possible.

Thanks in advance to anyone who takes the time to help me out here.

Regards.
Nov 13 '05 #2
Hi David,

Thanks very much for your help. This just about does it however I have
noticed that if the dates are:

1/4/05 and 31/3/08 the answer it gives is: 3 -1 30 where I think
it should be 2 11 30

Everything else seems to be spot on. Could you help me get around this

Nov 13 '05 #3
If the pure query approach gets too messy you can try pasting the
following code into a module and then use:

Years: MyDateDiff("yyy y",[DateObtained],[DateOfExpiry])
Months: MyDateDiff("m",[DateObtained],[DateOfExpiry])
Days: MyDateDiff("d",[DateObtained],[DateOfExpiry])

---8<------8<------8<------8<------8<------8<------8<---

Public Function MyDateDiff(Inte rval, date1, date2)
Dim years, months, days, d1, d2
If IsNull(date1) Or IsNull(date2) Then
MyDateDiff = Null
Exit Function
End If
' ensure that d1 precedes d2
If date1 < date2 Then
d1 = date1
d2 = date2
Else
d1 = date2
d2 = date1
End If
years = DateDiff("yyyy" , d1, d2)
months = DateDiff("m", d1, d2)
If months < 0 Then
years = years - 1
months = months + 12
End If
days = DateDiff("d", d1, d2)
Select Case Interval
Case "d"
MyDateDiff = days
Case "m"
MyDateDiff = months
Case Else
MyDateDiff = years
End Select
End Function

Nov 13 '05 #4
Carl:

I adjusted the Years calculation to adjust for the month of the origin date
being greater than the expire date. This is the same logic I applied to the
Months calculation. With the adjustment, I return 2 11 30 for your example.

SELECT TestDate.DateOb tained, TestDate.DateEx pired,
IIf(Month([DateObtained])<=Month([DateExpired]),DateDiff("yyy y",[DateObtained],[DateExpired]),DateDiff("yyy y",[DateObtained],[DateExpired])-1)
AS Years,
IIf(Day([DateObtained])<=Day([DateExpired]),DateDiff("m",[DateObtained],[DateExpired])-[Years]*12,DateDiff("m ",[DateObtained],[DateExpired])-[Years]*12-1)
AS Months,
AS Days
FROM TestDate;

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.
<ca**********@n ewcastle.gov.uk > wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
Hi David,

Thanks very much for your help. This just about does it however I have
noticed that if the dates are:

1/4/05 and 31/3/08 the answer it gives is: 3 -1 30 where I think
it should be 2 11 30

Everything else seems to be spot on. Could you help me get around this

Nov 13 '05 #5
Hi David,

Thanks again for your response. I managed to get the same answer as
you using the following:

SELECT tblIncident.Dat eObtained, tblIncident.Dat eOfExpiry,
DateDiff("yyyy" ,[DateObtained],[DateOfExpiry])-IIf(Format([DateObtained],"mmdd")>Format ([DateOfExpiry],"mmdd"),1,0 )
AS Years,
IIf(Day([DateObtained])<=Day([DateOfExpiry]),DateDiff("m",[DateObtained],[DateOfExpiry])-[Years]*12,DateDiff("m ",[DateObtained],[DateOfExpiry])-[Years]*12-1)
AS Months,
AS Days
FROM tblIncident;

Thanks again for taking the time in helping me out.

Could you tell me how to list the output so that it does not show
#Error if I have no DateObtained or DateOfExpiry in the table.

You gave me an example of putting this into a module. Can it be done
in a module using the SQL statements we have come up with as your
module version didn't give an error when fields were empty.

Thanks again for any pointers.

Regards.

Nov 13 '05 #6
Hi,

It's OK about the #Error thing, I asked the question before I put any
thought into it ... Sorry.

Here's what I put, don't know if it's an efficient way of doing it but
it seemed to work.

Cheers and thanks again for your time.

SELECT tblIncident.Dat eObtained, tblIncident.Dat eOfExpiry,
IIf([DateObtained] Is Null Or [DateOfExpiry] Is
Null,"",DateDif f("yyyy",[DateObtained],[DateOfExpiry])-IIf(Format([DateObtained],"mmdd")>Format ([DateOfExpiry],"mmdd"),1,0 ))
AS Years,
IIf([DateObtained] Is Null Or [DateOfExpiry] Is
Null,"",IIf(Day ([DateObtained])<=Day([DateOfExpiry]),DateDiff("m",[DateObtained],[DateOfExpiry])-[Years]*12,DateDiff("m ",[DateObtained],[DateOfExpiry])-[Years]*12-1))
AS Months,
IIf([DateObtained] Is Null Or [DateOfExpiry] Is
AS Days
FROM tblIncident;

Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.