Skip to main content
Participating Frequently
August 1, 2023
Answered

formatting time field H or HMM to HH:MM

  • August 1, 2023
  • 1 reply
  • 1346 views

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.

 

 

 

This topic has been closed for replies.
Correct answer bebarth

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);
}

 

@+

1 reply

bebarth
Community Expert
bebarthCommunity ExpertCorrect answer
Community Expert
August 1, 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);
}

 

@+

Participating Frequently
August 2, 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

try67
Community Expert
Community Expert
August 2, 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.