473,699 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:
DateDiff("d",Da teAdd("m",[Months],[DateObtained]),[DateOfExpiry])

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,
DateDiff("d",Da teAdd("m",[Months],DateAdd("yyyy" ,[Years],[DateObtained])),[DateExpired])
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:
DateDiff("d",Da teAdd("m",[Months],[DateObtained]),[DateOfExpiry])

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
please.

Thanks again for your help.

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)
d1 = DateAdd("yyyy", years, d1)
months = DateDiff("m", d1, d2)
If months < 0 Then
years = years - 1
months = months + 12
d1 = DateAdd("yyyy", -1, d1)
End If
d1 = DateAdd("m", months, d1)
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,
DateDiff("d",Da teAdd("m",[Months],DateAdd("yyyy" ,[Years],[DateObtained])),[DateExpired])
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
please.

Thanks again for your help.
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,
DateDiff("d",Da teAdd("m",[Months],DateAdd("yyyy" ,[Years],[DateObtained])),[DateOfExpiry])
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
Null,"",DateDif f("d",DateAdd(" m",[Months],DateAdd("yyyy" ,[Years],[DateObtained])),[DateOfExpiry]))
AS Days
FROM tblIncident;

Nov 13 '05 #7

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

Similar topics

13
3925
by: David Gray | last post by:
Greetings all, Quick newbie type question: I would like to be able to trap non-numerical data entered into a textbox via CTRL+C and/or Shift+Insert. I realise that this data can be validated using the TEXTn_validate event but I would like to stop the user before it gets that far.
28
742
by: Steve | last post by:
Hi all How would I find out the average date when given a bunch of dates? For example, I want to find the average length in time from the following dates: ---------------------------------------------------- Start Date End Date 01/01/2004 12:50pm 02/01/2004 18:40pm 02/01/2004 13:40pm 02/01/2004 13:57pm 02/01/2004 14:30pm 02/01/2004 19:50pm
26
4406
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: starttime = Date.parse("Aug 10,2003, 07:07") sdt = new Date(starttime)
7
12810
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
6
31811
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the elapsed time and somehow convert the days to years w/out using the VB DLL? Thanks.
6
8051
by: Tom | last post by:
Hello, please help me, I need two write a C++ program that can calculate the number of days between two dates.
6
14446
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a letter. Can I use javascripting to create a webpage to allow a user to enter a letter and then click a button to find a future calendar date? I'm just not sure how much user interaction scripting allows. Does java scripting allow buttons, textfields...
9
7899
by: Pierre Quentel | last post by:
Hi all, I have searched in the standard distribution if there was a function to return the difference between 2 dates expressed like an age : number of years, of months and days. The difference between datetime instances returns a timedelta object that gives a number of days, but not an age So is there such a function somewhere ? If not, for what reason, since it's a rather usual task
15
6428
by: student4lifer | last post by:
Hello, I have 2 time fields dynamically generated in format "m/d/y H:m". Could someone show me a good function to calculate the time interval difference in minutes? I played with strtotime() but but that only gave me difference in hours and only if the times were on the same day (after wrapping with date() function). TIA
0
8618
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8916
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
7752
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5875
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3058
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
2
2348
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2010
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.