Keep leading zeroes in a calculated numerical string in a text field

Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Hi there,

I have recently been developing digital pdfs for salespeople to use to process orders. These are very nearly complete.

One aspect of them is a unique ID agreement number. The format of this is as follows: aaa/bccccc

aaa is a predefined personnel number.

b is the last digit of the year - this currently does not update automatically and is just set as "7", and I will manually have to change this in 2018.

ccccc is a calculation of how many minutes we are into the year, divided by 5.25 (to keep the number as 99,999 or less during work days of the year, so this number is never more than five digits)

The problem I'm facing is that if ccccc is below five digits then the agreement number will be too short. Is there any way that I can force this part of the script to return five digits including leading zeroes? The calculation script I'm currently using is below:

var dd = "01/01/17 00:00";

var rd = util.printd("dd/mm/yy HH:MM", new Date());

var d1 = util.scand("dd/mm/yy HH:MM", dd);

var d2 = util.scand("dd/mm/yy HH:MM", rd);

var diff = (d2.valueOf() - d1.valueOf()) / 1000;

event.value = "326/7" + Math.round((diff / 60 /5.25));

In the above coding "326" is the personnel number so this is always set manually.

Whilst I'm here, is there also a way that the b section of the equation (the last digit of the year) can be automatically updated each year as well?

Many thanks,

Tom Seager

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use something like:

util.printf("%05d", ccccc);

TOPICS
Acrobat SDK and JavaScript

Views

522

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Keep leading zeroes in a calculated numerical string in a text field

Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Hi there,

I have recently been developing digital pdfs for salespeople to use to process orders. These are very nearly complete.

One aspect of them is a unique ID agreement number. The format of this is as follows: aaa/bccccc

aaa is a predefined personnel number.

b is the last digit of the year - this currently does not update automatically and is just set as "7", and I will manually have to change this in 2018.

ccccc is a calculation of how many minutes we are into the year, divided by 5.25 (to keep the number as 99,999 or less during work days of the year, so this number is never more than five digits)

The problem I'm facing is that if ccccc is below five digits then the agreement number will be too short. Is there any way that I can force this part of the script to return five digits including leading zeroes? The calculation script I'm currently using is below:

var dd = "01/01/17 00:00";

var rd = util.printd("dd/mm/yy HH:MM", new Date());

var d1 = util.scand("dd/mm/yy HH:MM", dd);

var d2 = util.scand("dd/mm/yy HH:MM", rd);

var diff = (d2.valueOf() - d1.valueOf()) / 1000;

event.value = "326/7" + Math.round((diff / 60 /5.25));

In the above coding "326" is the personnel number so this is always set manually.

Whilst I'm here, is there also a way that the b section of the equation (the last digit of the year) can be automatically updated each year as well?

Many thanks,

Tom Seager

Adobe Community Professional
Correct answer by Bernd Alheit | Adobe Community Professional

Use something like:

util.printf("%05d", ccccc);

TOPICS
Acrobat SDK and JavaScript

Views

523

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 04, 2017 0
Adobe Community Professional ,
Jul 04, 2017

Copy link to clipboard

Copied

Use something like:

util.printf("%05d", ccccc);

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 2
Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Spot on, thank you!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 0
Most Valuable Participant ,
Jul 04, 2017

Copy link to clipboard

Copied

This code will return the last digit of the current year, but keep in mind that it's going to repeat every ten years... That might seem far away in the future at the moment, but sooner or later you'll have problems knowing if "5" means "2005", "2015" or "2025"... I recommend you use at least two digits for the year.

At any rate, the code is:

util.printd("yyyy", new Date()).charAt(3)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 2
Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Thank you so much! Great stuff.

I agree with your thinking, however the agreement number just needs to be a unique identifier for agreements, and the date of signing is input onto the agreements separately as well. Our salespeople only sell on Thursdays and Fridays so the chance of a repeat number in ten years is very unlikely.

Have a great day!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 0
Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

try67, in light of your excellent answer, could you also let me know if there is a way to automate the beginning var dd in my code:

var dd = "01/01/17 00:00";

so that this automatically updates year after year also?

Thanks again,

Tom

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 0
Most Valuable Participant ,
Jul 04, 2017

Copy link to clipboard

Copied

Do you mean that it's always set as the first day of the current year?

If so, you can do it like this:

var dd = "01/01/" + util.printd("yy", new Date()) + " 00:00";

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 2
Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Excellent! Thank you again.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 0
Community Beginner ,
Jul 04, 2017

Copy link to clipboard

Copied

Would:

var dd = "01/01/1" + (util.printd("yyyy", new Date()).charAt(3)) + " 00:00";

​do the trick?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 0
Most Valuable Participant ,
Jul 04, 2017

Copy link to clipboard

Copied

Your code would only work until 2019... Use the version I posted above.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 04, 2017 2