• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Jul 04, 2017 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

TOPICS
Acrobat SDK and JavaScript

Views

868

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
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 04, 2017 Jul 04, 2017

Use something like:

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

Votes

Translate

Translate
Community Expert ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Use something like:

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

Votes

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
community guidelines
Community Beginner ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

Spot on, thank you!

Votes

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
community guidelines
Community Expert ,
Jul 04, 2017 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)

Votes

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
community guidelines
Community Beginner ,
Jul 04, 2017 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!

Votes

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
community guidelines
Community Beginner ,
Jul 04, 2017 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

Votes

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
community guidelines
Community Expert ,
Jul 04, 2017 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";

Votes

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
community guidelines
Community Beginner ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

LATEST

Excellent! Thank you again.

Votes

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
community guidelines
Community Beginner ,
Jul 04, 2017 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?

Votes

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
community guidelines
Community Expert ,
Jul 04, 2017 Jul 04, 2017

Copy link to clipboard

Copied

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

Votes

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
community guidelines