Calculating Difference between dates (1 Viewer)

ananga

New member
Local time
, 01:17
Joined
Jun 20, 2005
Messages
5
In access in one field I have one date and in another field I have another date. I want to calculate the number of years.,months and days elapsed between these two date. e.g. suppose in one field I have 30.06.2005 and in other 20.04.2004 then it should show 1 year 2 months and 10 days.
 

MStef

Registered User.
Local time
Today, 20:47
Joined
Oct 28, 2004
Messages
2,251
Hello ananga!
Look at "DemoYMDA2000.mdb"
 

Attachments

  • DemoYMDA2000.zip
    14.9 KB · Views: 349

Bat17

Registered User.
Local time
Today, 20:47
Joined
Sep 24, 2004
Messages
1,687
Mstef
Try the dates 31 Dec 2004 to 1 Jan 2005 :D

Peter
 

MStef

Registered User.
Local time
Today, 20:47
Joined
Oct 28, 2004
Messages
2,251
Thanks BAT17,
here is a new one.
 

Attachments

  • DemoYMDA2000.zip
    15 KB · Views: 235

EMP

Registered User.
Local time
Today, 20:47
Joined
May 10, 2003
Messages
574
MStef,

31 May 2003 to 1 April 2005 returned Year 2 Month -1 Day -29

^
 
Last edited:

MStef

Registered User.
Local time
Today, 20:47
Joined
Oct 28, 2004
Messages
2,251
Thanks EMP!
Yes you are right. It happens when it was not tested
enough, (I was in a hurry). I think it's OK now.
 

Attachments

  • DemoYMDA2000.zip
    15.8 KB · Views: 243

jamjarr

Registered User.
Local time
, 03:47
Joined
Apr 30, 2005
Messages
17
Thank you for supplying the sample database

I am a newbie with Access but an oldie in age.

The sample code works the way that I want but how can I write the values into separate fields called say VMonth,VDay.

I have two fields called StartDate and EndDate and I want to calculate the difference in months and years.

Thanks in antipication.:)
 

MStef

Registered User.
Local time
Today, 20:47
Joined
Oct 28, 2004
Messages
2,251
Hello jamjarr!

I don't understand what you want.
This demo work a diferent in years, months and days.
 

jamjarr

Registered User.
Local time
, 03:47
Joined
Apr 30, 2005
Messages
17
Sorry for the misunderstanding

The database works fine for me.

However I want to extend the functionality by having the values in the years months and days text boxes in the form. I was wanting to use these values for other uses so i was wanting to write these values to designated fields in the table. Say I have fields in the table called vYears, Vmonths and Vdays and the values in your text boxes are written to those fields when I press a command button called "Save Record".

Any ideas.

Thanks.
 

DonKHotay

New member
Local time
Today, 15:47
Joined
Oct 18, 2004
Messages
32
ananga said:
In access in one field I have one date and in another field I have another date. I want to calculate the number of years.,months and days elapsed between these two date. e.g. suppose in one field I have 30.06.2005 and in other 20.04.2004 then it should show 1 year 2 months and 10 days.


Hi there,
You can easily break apart the date field with the functions Month(), Day(), Year(). So to get the breakdown of elapsed years, months, days add these three fields to your query: Year(DateField1) - Year(DateField2), Month(DateField1) - Month(DateField2), Day(DateField1) - Day(DateField2).
 

EMP

Registered User.
Local time
Today, 20:47
Joined
May 10, 2003
Messages
574
DonKHotay said:
Hi there,
You can easily break apart the date field with the functions Month(), Day(), Year(). So to get the breakdown of elapsed years, months, days add these three fields to your query: Year(DateField1) - Year(DateField2), Month(DateField1) - Month(DateField2), Day(DateField1) - Day(DateField2).

DonKHotay,

The expressions may return negative numbers. For instance
Code:
DateField2	DateField1	Years	Months	Days
12/31/2004	1/1/2005	1	-11	-30

^
 

MStef

Registered User.
Local time
Today, 20:47
Joined
Oct 28, 2004
Messages
2,251
Hello Jamjarr!

It's possible, look at "DemoYMD2A2000.mdb",
Try to add a new record, but it's not necessary,
because you can calculate this always yuo need it.
 

Attachments

  • DemoYMD2A2000.zip
    17.3 KB · Views: 311

jamjarr

Registered User.
Local time
, 03:47
Joined
Apr 30, 2005
Messages
17
Calculating Dates

Thank you very much.

It works wonderfully.

Now to the next hurdle.

Regards
 

DonKHotay

New member
Local time
Today, 15:47
Joined
Oct 18, 2004
Messages
32
EMP said:
DonKHotay,

The expressions may return negative numbers. For instance
Code:
DateField2	DateField1	Years	Months	Days
12/31/2004	1/1/2005	1	-11	-30

^


true....I guess to do it that way you would need to get the absolute value so Abs(Year()), Abs(Month()), and ABS(Day())
 

EMP

Registered User.
Local time
Today, 20:47
Joined
May 10, 2003
Messages
574
DonKHotay said:
true....I guess to do it that way you would need to get the absolute value so Abs(Year()), Abs(Month()), and ABS(Day())
Abs() can remove the negative signs from the numbers. But it can't return the correct numbers.

^
 

DonKHotay

New member
Local time
Today, 15:47
Joined
Oct 18, 2004
Messages
32
hehe, you are right of course....I did not think this through entirely. I guess it's been tackled already but had I attempted this in a more thoughtful manner I might've gone about using lots of Iif's and nested Iif statements. First there would be a test for Leap year, because if there is a leap year involved you would be dividing the difference of the two date fields by 366 instead of 365, and then taking the floor of that to get years elapsed.

... I forsee many problems with that approach, so I would have ended up asking for help from the pros here!
 

Users who are viewing this thread

Top Bottom