Skip to main content
tom.seager
Participating Frequently
July 4, 2017
Answered

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

  • July 4, 2017
  • 2 replies
  • 1452 views

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

This topic has been closed for replies.
Correct answer Bernd Alheit

Use something like:

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

2 replies

try67
Community Expert
Community Expert
July 4, 2017

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)

tom.seager
Participating Frequently
July 4, 2017

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!

tom.seager
Participating Frequently
July 4, 2017

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

Bernd Alheit
Community Expert
Bernd AlheitCommunity ExpertCorrect answer
Community Expert
July 4, 2017

Use something like:

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

tom.seager
Participating Frequently
July 4, 2017

Spot on, thank you!