Step 1: Check us out

You don't have to be a member to look at any content on the site. Increase your expertise with our helpful tutorials, videos, forums, and sample PDFs.

Step 2: Sign up for a free account

Like what you see? Take the next step and become a member. Register now to get discounts, attend eSeminars, ask questions and more.

Step 3: Start participating

Get the most out of your membership. Post in the forums, create your profile, submit to the gallery, attend a user group meeting. Log In now.

Acrobat User Community Forums

You are not logged in.     Log in to your AUC account.     Don't have an account? Sign up today

#1 2010-02-08 17:07:59

shawnash
Member
Registered: 2009-12-17
Posts: 19

Years between 2 dates Calculation

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 / Windows

Offline

 

#2 2010-02-08 18:33:16

try67
Expert

Registered: 2008-10-30
Posts: 1105

Re: Years between 2 dates Calculation

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:

Code:

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)

Last edited by try67 (2010-02-08 18:35:07)


Check out my custom-made scripts website: http://try67.blogspot.com

Offline

 

#3 2010-02-08 19:46:57

shawnash
Member
Registered: 2009-12-17
Posts: 19

Re: Years between 2 dates Calculation

Ok, thanks for the response and I will try it.  But, how do you get today's date?

Offline

 

#4 2010-02-09 03:08:32

try67
Expert

Registered: 2008-10-30
Posts: 1105

Re: Years between 2 dates Calculation

When you create a new Date object it automatically has the current date and time.


Check out my custom-made scripts website: http://try67.blogspot.com

Offline

 

#5 2010-02-09 10:19:27

shawnash
Member
Registered: 2009-12-17
Posts: 19

Re: Years between 2 dates Calculation

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

Offline

 

#6 2010-02-09 11:47:26

try67
Expert

Registered: 2008-10-30
Posts: 1105

Re: Years between 2 dates Calculation

ACCEPTED ANSWER

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:

Code:

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;

Check out my custom-made scripts website: http://try67.blogspot.com

Offline

 

#7 2010-02-09 12:43:07

shawnash
Member
Registered: 2009-12-17
Posts: 19

Re: Years between 2 dates Calculation

Thank you so much, that worked perfectly!  :)

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson