Time field entry for duration

An interesting subject came up in another post about this.
I have a list of songs in a table. One of the fields is duration, which is entered-not calculated.
The field is set up as a Time field.

image

During data entry, the user must enter 00:04:30 (above), and on the layout the field is formatted as shown in the left grab. If I leave the field and go back to it, I see the dataformat on the right.
image .image

Ultimately, I'm looking for an easier way to enter the duration as xx:xx:xx is fairly cumbersome.
Any ideas?

John Renfrew developed a little app for me that relies on typing time. You enter the time the same way you would on a microwave.

microwave.fmp12 (472 KB)

2 Likes

Sweet. I'll have to give it a whirl.

Here a CF FormatAsTime( ~time ): with a check whether the input works as a time entry.

[Edit:] With this custom function, you only need to enter the numbers. Without colons and leading zeros before the first number.

For entering 4 minutes and 25 seconds just type 425. The CF gives back 00:04:25.

Let ( [
	~time = Substitute( ~time ; ":" ; "" );
	~lengthOfTimeString = Length( ~time );
	~timeWithLeadingZeros = Right ( "0000000000" & ~time ; ~lengthOfTimeString );
	~hours = Left ( ~timeWithLeadingZeros ; ~lengthOfTimeString - 4 );
	~minutes = Middle ( ~timeWithLeadingZeros ; 3 ; 2 );
	~seconds = Right ( ~timeWithLeadingZeros ; 2 );
	~timeFormatted = ~hours & ":" & ~minutes & ":" &  ~seconds;
	~validateInput = If ( GetAsNumber ( ~hours ) < 24 AND GetAsNumber ( ~minutes ) < 60 AND GetAsNumber ( ~seconds ) < 60 ; true ; false );
~end = ""];
	// Calculation
	If ( ~validateInput ; ~timeFormatted ; "?")
)

Call the CF in the auto enter calculation of the field.
[Edit:] Make sure to uncheck "Do not replace existing field value"
[Edit:] Now the CF is on Brian Dunnings website: https://www.briandunning.com/cf/2460
[Edit:] I have added the substitution of colons at the beginning of the cf. Now you can modify the time in the field in the case you have mistyped.
[Edit:] :crazy_face: Call me the "500TimesOneMoreThing-Guy"! Every few minutes there’s something I have to add or correct. This time I have corrected a mistake in the cf.
[Edit – once more:] Added a check if the string is longer than 6 digits and handles that. Before the string simply was cutted. Now it returns a "?" if the string is too long.

5 Likes

This is brilliant!

1 Like

@mipiano - can you help me out a bit with this - I LOVE THE IDEA!

My field name is "duration" so should I replace "~time" with "duration"?

I currently get a "?" in the field.

To call the custom function in the auto enter calculation of your field "duration" you write "FormatAsTime( Self )". So the value you enter into the field is given to the custom function that returns the formatted value.

Every time you enter a too long string or a minute value with more than 59 minutes or seconds with more than 59 seconds the cf returns a "?" to show there’s an error.

[Edit:] But first you have to create the custom function. Did you do that?

To create the custom function go to "File > Manage > Custom Functions…"
Click "New" – enter "FormatAsTime" into the field "Function Name". In the field below you type "~time". Then click on the + on the right side of the field. Into the big field you copy the code of the new function:

/*
Typing times into time fields is cumbersome: ##:##:##

With this custom function, you only need to enter the numbers. Without colons and leading zeros before the first number.

For entering 4 minutes and 25 seconds just type 425. The CF gives back 00:04:25.

Call this CF in the auto enter calculation of a time field and make sure to uncheck "Do not replace existing field value".
*/

    Let ( [
    	~time = Substitute( ~time ; ":" ; "" );
    	~lengthOfTimeString = Length( ~time );
    	~timeWithLeadingZeros = Right ( "0000000000" & ~time ; ~lengthOfTimeString );
    	~hours = Left ( ~timeWithLeadingZeros ; ~lengthOfTimeString - 4 );
    	~minutes = Middle ( ~timeWithLeadingZeros ; 3 ; 2 );
    	~seconds = Right ( ~timeWithLeadingZeros ; 2 );
    	~timeFormatted = ~hours & ":" & ~minutes & ":" &  ~seconds;
    	~validateInput = If ( GetAsNumber ( ~hours ) < 24 AND GetAsNumber ( ~minutes ) < 60 AND GetAsNumber ( ~seconds ) < 60 ; true ; false );
    ~end = ""];
    	// Calculation
    	If ( ~validateInput ; ~timeFormatted ; "?")
    )

Click the OK button and then the OK button in the Manage Custom Functions window and you’re done! Now you can use the custom function as described in my previous answer.

Hope that helps.

2 Likes

Sorry I'm such a noobie - but I'm learning....
I added the CF and entered the FormatAsTime(Self) statement in the calculation. I duplicated the duration field for testing (duration copy) until I get it worked out.
image
The first left field is my original; the right is the duration copy.
I've changed the field type from time to text to number all with the same results (I entered 401 in the second field).

What am I missing?

Ok – I have found my mistake. The function has to check wether the entered string has up to 6 digits or more. Here’s the corrected function:

Let ( [
	~time = Substitute( ~time ; ":" ; "" );
	~lengthOf~time = Length( ~time );
	~lengthOfTimeString = If( ~lengthOf~time ≤ 6 ; 6 ; ~lengthOf~time ); 
	~timeWithLeadingZeros = Right ( "0000000000" & ~time ; ~lengthOfTimeString );
	~hours = Left ( ~timeWithLeadingZeros ; ~lengthOfTimeString -4 );
	~minutes = Middle ( ~timeWithLeadingZeros ; ~lengthOfTimeString -3 ; 2 );
	~seconds = Right ( ~timeWithLeadingZeros ; 2 );
	~validateInput = If ( GetAsNumber ( ~hours ) < 24 and GetAsNumber ( ~minutes ) < 60 and GetAsNumber ( ~seconds ) < 60 ; True ; False );
	~timeFormatted = ~hours & ":" & ~minutes & ":" &  ~seconds;
~end = ""];
	// Calculation
	If ( ~validateInput ; ~timeFormatted ; "?")
)

In my test the type of the duration field is time and the data formatting in the Inspector is mm:ss but can also be hh:mm:ss.

Sorry for the confusion!

2 Likes

No worries - it's a wonderful solution.
Can I still calculate on it (i.e., sum of time)?

Yes. It is still a time field. The CF only inserts the leading zeros and the colons for you.

1 Like

Completely, 100% brilliant!!!

1 Like

Thank you. I'm glad if it's useful for you. It's just a little thing - nothing special.