Skip to main content
Participating Frequently
July 4, 2025
Answered

Keystroke script to display string into HH:MM:ss tt

  • July 4, 2025
  • 1 reply
  • 272 views

I found a keystroke script that would convert a 4 digits into HH:MM format, but want to go beyond that and convert to HH:MM:ss tt (e.g. 1645 into 4:45:00 PM.) Here is the script I currently use.

 

if(event.willCommit)

{

event.value = event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");

}

Correct answer try67

That's because the padStart method is not supported in Acrobat, and also the willCommit property is not relevant here.

Use this code:

 

if (event.value && event.value.length === 4) {
    var raw = event.value.replace(/\D/g, ''); // Remove non-digits
    var hh = parseInt(raw.substr(0, 2), 10);
    var mm = parseInt(raw.substr(2, 2), 10);
    var ss = "00";
    var period = "AM";

    if (hh >= 12) {
        period = "PM";
        if (hh > 12) hh -= 12;
    }
    if (hh === 0) hh = 12;

    // Format with leading zeros if needed
    var hStr = hh.toString();
    var mStr = mm.toString();
	if (mStr.length==1) mStr = "0"+mStr;

    event.value = hStr + ":" + mStr + ":" + ss + " " + period;
}

 

1 reply

Legend
July 4, 2025

Hello, 

Thanks for reaching out. Try the following:

Use this in the Format event (not keystroke) for better control:

if (event.value && event.value.length === 4 && event.willCommit) {
    var raw = event.value.replace(/\D/g, ''); // Remove non-digits
    var hh = parseInt(raw.substr(0, 2), 10);
    var mm = parseInt(raw.substr(2, 2), 10);
    var ss = "00";
    var period = "AM";

    if (hh >= 12) {
        period = "PM";
        if (hh > 12) hh -= 12;
    }
    if (hh === 0) hh = 12;

    // Format with leading zeros if needed
    var hStr = hh.toString();
    var mStr = mm.toString().padStart(2, '0');

    event.value = hStr + ":" + mStr + ":" + ss + " " + period;
}

Input

Output

0845

8:45:00 AM

0000

12:00:00 AM

1200

12:00:00 PM

1645

4:45:00 PM

2359

11:59:00 PM

 

How to Apply in Acrobat:

  1. Open the form field properties.

  2. Go to the Format tab → Select Custom Format Script.

  3. Click Edit and paste the script above.

 

 

If the customer prefers to use it in the Keystroke script, it can be modified slightly to support real-time typing but may behave unpredictably during partial inputs.




~Tariq

Participating Frequently
July 4, 2025

I just did and the output value is still the same as the input. It only shows the 0845, it doesn't convert it.

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
July 4, 2025

That's because the padStart method is not supported in Acrobat, and also the willCommit property is not relevant here.

Use this code:

 

if (event.value && event.value.length === 4) {
    var raw = event.value.replace(/\D/g, ''); // Remove non-digits
    var hh = parseInt(raw.substr(0, 2), 10);
    var mm = parseInt(raw.substr(2, 2), 10);
    var ss = "00";
    var period = "AM";

    if (hh >= 12) {
        period = "PM";
        if (hh > 12) hh -= 12;
    }
    if (hh === 0) hh = 12;

    // Format with leading zeros if needed
    var hStr = hh.toString();
    var mStr = mm.toString();
	if (mStr.length==1) mStr = "0"+mStr;

    event.value = hStr + ":" + mStr + ":" + ss + " " + period;
}