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

formatting time field H or HMM to HH:MM

New Here ,
Aug 01, 2023 Aug 01, 2023

Hi,

I'm trying convert the time entered as a number in the Format H.MM or H,MM or HMM or only a H number to this Format HH:MM.

 

I found following script in this Forum:

 

if(event.willCommit)

{

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

}

 

I have faild to edit the values in between the brackets.

Any help would be appreciate.

I have tried to select format Category HH:MM in Text field Properties but this was not helpful for me.

 

 

 

TOPICS
Acrobat SDK and JavaScript , Windows
1.3K
Translate
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 , Aug 01, 2023 Aug 01, 2023

Hi,

In custom format script:

 

 

switch(event.value.toString().replace(/[\,\.]/,"").length) {
	case 4:
		event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");
		break;
	case 3:
		event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"0$1:$2");
		break;
	case 2:
		event.value+=":00";
		break;
	case 1:
		event.value="0"+event.value+":00";
		break;
	default:
		event.value="";
}

 

 

See the attached example file.

But you should have a custom keystroke script t

...
Translate
Community Expert ,
Aug 01, 2023 Aug 01, 2023

Hi,

In custom format script:

 

 

switch(event.value.toString().replace(/[\,\.]/,"").length) {
	case 4:
		event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");
		break;
	case 3:
		event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"0$1:$2");
		break;
	case 2:
		event.value+=":00";
		break;
	case 1:
		event.value="0"+event.value+":00";
		break;
	default:
		event.value="";
}

 

 

See the attached example file.

But you should have a custom keystroke script too, to only allow correct hours (from 0 to 11) and minutes (from 00 to 59)!

Here is the custom keystroke script:

 

if (!event.willCommit) {
	var aTester=event.value.split("");
	aTester.splice(event.selStart, event.selEnd-event.selStart, event.change);
	var testeChaine=aTester.join("");
	var RegExTime=/^((([0]?\d)|([1][0-1]?))(([,]|[.])?([0-5]\d?)?)?)?$/;
	event.rc=RegExTime.test(testeChaine);
} else {
	var RegExTime=/^(([0]?\d)|([1][0-1]))(([,]|[.])?[0-5]\d)?$/;
	event.rc=event.value=="" || RegExTime.test(event.value);
}

 

@+

Translate
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
New Here ,
Aug 02, 2023 Aug 02, 2023

Thanks it works perfect for me as i entered the first Code in Keystroke script. Because i need it to change the value to the needed format (there are other Calculation that depends on the HH:MM Format)

 

one last problem : how can i set a timer of  2 seconds till the script be calculated?

for Exampel: if i enter the value 11 it waits 2 Seconds  and then convert it to 11:00 Format. even if i click on other fields.

 

What i tried is follwing code:

 

setTimeOut(function() {
switch(event.value.toString().replace(/[\,\.]/,"").length) {
case 4:
event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");
break;
case 3:
event.value=event.value.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"0$1:$2");
break;
case 2:
event.value+=":00";
break;
case 1:
event.value="0"+event.value+":00";
break;
default:
event.value="";
}
}, 2000);

 

Many Thanks

Translate
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 ,
Aug 02, 2023 Aug 02, 2023

Several issues:

1. It's app.setTimeOut, not just setTimeOut.

2. The first parameter needs to be a string, not a literal function.

3. You should use a variable to store the return value of calling this function, even if you don't plan on cancelling it. I've seen cases where it didn't work if you don't do that.

Translate
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
New Here ,
Aug 02, 2023 Aug 02, 2023

I would be very greatful if you tell me how the code will be. That iam new to scripting.

Translate
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 ,
Aug 02, 2023 Aug 02, 2023
LATEST

Hi,

You must write like that:

 

// Custom Format Script
function theScript(theValue,fieldName) {
	switch(theValue.toString().replace(/[\,\.]/,"").length) {
		case 4:
			this.getField(fieldName).value=theValue.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"$1:$2");
			break;
		case 3:
			this.getField(fieldName).value=theValue.toString().replace(/(\d{1,2})[\,\.]?(\d{2})/,"0$1:$2");
			break;
		case 2:
			this.getField(fieldName).value=theValue+":00";
			break;
		case 1:
			this.getField(fieldName).value="0"+theValue+":00";
			break;
		default:
			this.getField(fieldName).value="";
	}
}
theValue=event.value;
fieldName=event.target.name;
run=app.setTimeOut("theScript(theValue,fieldName);app.clearTimeOut(run);",2000); // 2s = 2000ms

 

@+

Translate
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