These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Years between 2 dates Calculation

shawnash
Registered: Dec 16 2009
Posts: 48
Answered

Hello,

I have a field called HireDt that is formatted as date and mm/dd/yyyy. I want to compare HireDt with today's date and come up with YrsSvc calculated field with only the full years between.

Any help would be appreciated.

Thanks, Shawn

My Product Information:
Acrobat Standard 5.x or older, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
There are plenty of tutorials on how to work with Date objects on this website. I suggest you search for them. But basically, you need to do something like this:

date = util.scand("mm/dd/yyyy",dateAsString);difference_in_years = date.getFullYear() - (new Date().getFullYear());

(edit: this will just show the difference in whole years, if you want to take the actual day into account it needs to be tweaked a bit)

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

shawnash
Registered: Dec 16 2009
Posts: 48
Ok, thanks for the response and I will try it. But, how do you get today's date?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
When you create a new Date object it automatically has the current date and time.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

shawnash
Registered: Dec 16 2009
Posts: 48
Thank you - this almost works. I know I am probably missing something really simple here, but I entered 03/10/2000 into my HireDt field and my YrsSvc field should have come up to 9 years from today's date because we have not reached March 10th yet. But it is rounding up and I am getting 10 years instead.

This is my code.
var Hire = this.getField('HireDt').value;
date = util.scand("mm/dd/yyyy",Hire);
difference_in_years = (new Date().getFullYear()) - date.getFullYear();
event.value = difference_in_years;

Any help is appreciated.
Thanks, Shawn
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Well, like I said, the other script was not perfect, because it didn't take into account the day of the year.
This version does:
var Hire = this.getField('HireDt').value;date = util.scand("mm/dd/yyyy",Hire);now = new Date();difference_in_years = (now.getFullYear()) - date.getFullYear();if (now.getMonth()<date.getMonth()) {difference_in_years--;} else if ((now.getMonth()==date.getMonth()) && (now.getDate()<date.getDate())) {difference_in_years--;}event.value = difference_in_years;

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

shawnash
Registered: Dec 16 2009
Posts: 48
Thank you so much, that worked perfectly! :)