Brian Dunning's FileMaker Custom Functions

TimeFormatAs ( theTime ; type12or24 )

Formats time data as 12 or 24 hour time at the data level, rather than field format display level. Especially useful for users that desire 12-hour formatted time data.

  Average rating: 4.3 (50 votes) Log in to vote

Jonathan Mickelson   Jonathan Mickelson - Show more from this author

Share on Facebook Share on Twitter

  Sample input:
TimeFormatAs ( "10:30 AM" ; 24 )
TimeFormatAs ( "00:30:00" ; 12 )
TimeFormatAs ( "16:00" ; 12 )
TimeFormatAs ( "4:23 PM" ; 24 )
  Sample output:
"10:30:00"
"12:30 AM"
"4:00 PM"
"4:23 PM"

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

Modification Date: 4/12/2010
- Fixed behavior for better consitency due to changes in Time calculation results in FileMaker over the last few Versions (8-11).
- Fixed bug, reported by Will Loving: in 24 hr mode, "4:23 PM" time returned "4:23 PM" rather than the expected "16:23:00".
- Tweaked behavior to better handle valid FileMaker times like 11:65:65, to add extra any overage amounts of lesser time parts to their higher counterparts correctly, to make it a clock-compatible time, 12:06:05.
- Made separator between AM/PM and time a variable for easy output tweaking.

Modification Date: 2/12/2012
- Fixed behavior for better consitency due to changes in Time
calculation results in FileMaker over the last few Versions (8-11).
- Fixed bug, reported by Will Loving: in 24 hr mode, "4:23 PM" time returned "4:23 PM"
rather than the expected "16:23:00".
- Tweaked Behavior to better handle valid Filemaker times like 11:65:65, to add extra
overage amounts of lesser time parts to their higher counter parts correctly, to
make it a clock-compatible time.
- Made separator between AM/PM and time a variable for easy output tweaking.

Modification Date: 12/3/2012
- Escaped blank theTime input, was defaulting to 12AM.
- Fixed input parameter name not being correctly placed into theTime, variable.
- Renamed Time Input from timeField to theTime, since it doesn't require a field to use.

NOTE: Due to changes in the way FileMaker handles Time in calculations, there may be circumstances where you will find it difficult to get FileMaker to "display" the time as you intended. This calc will help, and in most cases it is all you need -- but if you are having problems, try looking at the custom function's results in the data viewer, or a text field, to see the actual results, rather than a time field.
--------------------

This function formats any valid FileMaker Pro time value by the format specified in the function's parameter allowing the user to convert from any time format to 12 or 24 hour time. This is useful because calculated time data results are always returned in FileMaker Pro's native "24 hour" time format.

As of version 7, this is compatible with Set Field, where previous versions were limited to using a Paste Result [calculated].

Though "Field Formatting" on a layout can format the time data in a nice 12 hour format when viewed onscreen, the nicely presented time reveals it's true 24 hour nature if a user clicks into the field (they will see "0:30" instead of "12:30 AM"). In the case of our the example above, any 12 hour formatted value lists will also fail to "open scrolled to" the proper "12:30 AM" value.

You can return a specific Time format if you wrap any time calculations you use with this function and you will know the user will see "nice 12 hour" time, this also works nicely on time fields with Auto-Enter and overwrite existing values set to "on".

NOTES:
Some changes to the AM/PM variables or other formatting options within the function may be necessary to make this compatible with other language/time settings for solutions run on systems that do not use U.S. 12-hour time formatting.

This function is not designed to espouse 12 hour time over 24 (or Military Time), but many users are disconcerted when data appears to have "lost its formatting," just because the system performed a calculation upon it.

----------------------
Parameters:

theTime - The time value to convert. The function only needs a time that is recognized by FileMaker Pro, and does not have to be the opposite of the desired converted result. (Thus if you are converting to 12 hour you *do not have to* provide a 24 hour formatted time.

type12or24 - The desired time format of the time result. The expected values are either 12 or 24, the default method is 12 Hour. Null or any value less than 24 will default to return a 12 hour time result .

 

Comments

Will Loving   Will Loving, Dedication Technologies, Inc.
Feb 18, 2010
This function does not properly display 24-hour time when the original input field is formatted as 12-hour time and the time value contains "am" or "pm" notations. For example: a "4pm" entry gives "4 PM" not "16:00:00", a "4:23 pm" entry gives "4:23 PM" not "16:23:00". This can be resolved by doing an explicit time to text concatenation such as:

Hour(timeField ) & ":" & Right( "00" & Minute(timeField) ; 2) & ":" & Right( "00" & second(timeField) ; 2);

instead of "GetAsTime( timeField)".

You can leave off the seconds if you wish or add a parameter to include or exclude them.
 
Denise   Denise, Boston
Apr 9, 2010
Just what I was looking for. Thank you!
 
Jonathan Mickelson   Jonathan Mickelson
Apr 12, 2010
UPDATED: 4/16/2010

Thanks to Bill and Denise for the feedback, I was able to fix the problem Bill found and made some enhancements to the display of seconds and other time evaluation issues. See Description above for more details.

The function of this calc remains unchanged though, save for the inclusion of seconds, in 12 hour mode, if non-zero seconds provided in the time parameter.
 
Rick Altman   Rick Altman, Pleasanton
Apr 2, 2012
I am unable to install this custom function -- FileMaker complains about a parameter not being found, and selects the

"asSecon"

inside of

"hasSeconds"

as the culprit. Any ideas...?
 
Jonathan   Jonathan, Los Angeles
Apr 3, 2012
Hi Rick,

I just copied and pasted the calc to a new file, seems to not have any problems... perhaps the copy and paste missed an end line ";" or one of the two params ( timeField ; type12or24 ) were not defined yet?

Seems to check out for me... let me know if it still has problems for you!

Jonathan
 
Robert Baker   Robert Baker, Columbus, OH USA
Dec 2, 2012
The problem is in the line above the error line:

theTime = GetAsTime ( theTime ) ;

This should say:

theTime = GetAsTime ( timeformat ) ;

Since it is looking at the input parameter. Otherwise it is meaningless.
 
Robert Baker   Robert Baker, Columbus, OH USA
Dec 2, 2012
Correction - Sorry! It should say:

theTime = GetAsTime ( timefield )

to match the input parameter name.
 
Jonathan   Jonathan, Van Nuys, CA
Dec 3, 2012
You are right, I somehow munged that during the last edits... thanks for catching that Robert!
 
Jonathan   Jonathan, Van Nuys, CA
Dec 3, 2012
Also, I've updated function in two ways:
- Include a short circuit if the time is empty, so it doesn't force blanks to display 12:00 AM, but remains blank.
- Renamed the input parameter so it doesn't say timeField, but rather theTime, since this does not require a field to work, any time value will be fine.
 
Jason Lecureux   Jason Lecureux
Dec 8, 2012
Many thanks,

Comment at start of function still reads the first param being 'timeField':


Syntax: TimeFormatAs ( timeField; type12or24 )

Should be:

Syntax: TimeFormatAs ( theTime; type12or24 )

didn't take long to figure out.
 
Jonathan   Jonathan, Van Nuys, CA
Jan 22, 2013
Thanks Jason, I missed that documentation change. The comment now matches the function parameter name.
 
tom   tom, ShiftPoint
Oct 1, 2015
Is there a way to suppress the seconds from the output? To be quite honest, I'm being a bit lazy and haven't read through the code in the script. I'm merely looking for an easy way to take a time field and output it in a 12 hour format.
 
Jonathan Mickelson   Jonathan Mickelson
Oct 1, 2015
Yes Tom, for the 12 hour time it's a little easier to do. Towards the bottom of the calculation you will see a Result12 variable... Towards the end of the case statements found inside that variable, you'll see the seconds section, which you can delete or modify with a variable to make it optional.

Hope that helps!
Jonathan
 
tom   tom, ShiftPoint
Oct 2, 2015
Thanks and sorry for being lazy. I've been swamped and took the easy way out. Have a great weekend!
 
Kimbakat   Kimbakat, NYC
Oct 12, 2017
If you do things incrementally...and MUST be able to create schedules and show BOTH or EITHER US or MILITARY for various items....

Format the field for TIME.
in Inspector Choose DATA and Format TIME as ENTERED.

Use a drop down menu for various times of the day US and MILITARY included in dropdown list.

Drop Down menu example in 15 min increments:

US LIST
-------------------------
00:00am
00:15am
00:30am
00:45am

01:00am
01:15am
01:30am
01:45am

etc.

For our purposes during work daylight hours..first common used 12 hours are listed in a drop down.

06:00pm
06:15pm

listed all the way to...

07:45pm

08:00pm
------------------------

We don't change up schedules so quickly we do everythign in 15 min intervals..so we use 15 min.

For MILITARY...during work hours..I created more time options JUST BELOW the US...to handle the afternoon hours..so I add to my dropdown list to cover the afternoon hours....

MILITARY LIST
-----------------
13:00
13:15
13:30

etc...to go up to 7pm or 19:00

--------------------

It takes a bit to make the drop down list complete..but the beauty of it is you can copy the time and paste it in another time field...and it keeps that time at the same spot in the dropdown list..so you when you pull down...you are already in the spot on the list in the time frame you are working with...so for incremental additional or adding time slots...its easy and fast.

No issues with scripts, triggers...formatting..etc...Sort order works..Ascending.
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: