6 Replies Latest reply on May 30, 2013 7:19 AM by philmodjunk

    Auto Formatting Time with script trigger

    DavidRoy_1

      Title

      Auto Formatting Time with script trigger

      Post

           Hi all,

           I have several time fields (Time of Day, Allow Override) on multiple layouts in my solution and am trying to make the solution friendlier to the users.  As such I'd like to write a script that will auto-correct and reformat times entered into the field into the way filemaker wants to see the data, so that the users aren't shown an error. 

           For example:  I'd like someone to be able to type 5, or 5pm, or 5 pm, or 5:30 pm, and in each instance the script will correct the data and display it as 5:30 pm

           I've searched around, and have found several leads, but haven't been able to find a good answer to this.  Is anyone willing to write a short how-to?

           Thanks.

        • 1. Re: Auto Formatting Time with script trigger
          philmodjunk

               I'd like someone to be able to type 5, or 5pm, or 5 pm, or 5:30 pm

               And how does the system know that 5 is for 5 pm and not 5 am?

               If you are using FileMaker 11 or newer, set up the on object validate trigger to perform a script that corrects the formatting before Filemaker's data validation can kick in and complain that the format is not a valid time entry.

          • 2. Re: Auto Formatting Time with script trigger
            DavidRoy_1

                 Right, that makes perfect sense.  I should have been clearer:

                 I can't figure out how to actually write the auto-correcting script.

            • 3. Re: Auto Formatting Time with script trigger
              philmodjunk

                   Actually, all of your examples except  that lone 5 will be correctly accepted as the time for 5:00 pm (or 5:30 pm) in a time field. Only the case where you need a value of 5 to be entered and 5:00 pm to be the result is there a need for special handling as it would be interpretted as the time for 5:00 am.

                   Thus, you could just set up an auto-enter calculation if you need to automatically convert am times into pm values:

                   IF ( Self < GetAsTime ( "12:00 pm" ) ; self + 12 * 3600 ; self )

                   This expression adds 12 hours to any am times to change them into pm times 12 hours later.

                   (Be sure to clear the "do not replace existing values..." check box.)

              • 4. Re: Auto Formatting Time with script trigger
                DavidRoy_1

                     ah but there's the problem.  It was my understanding that yes, Filemaker would recognize all the examples as 5pm except the 5, which it would recognize as a duration of 5 hours.  But whenever I enter a time in a field I get thrown this error.  This is the underlying problem, I would like to accept the data the user enters, correct it (if possible) and then have it validated and commited, so that this error doesn't occur.  Maybe I missed something in the field setup?

                      

                      

                • 5. Re: Auto Formatting Time with script trigger
                  DavidRoy_1

                       Partial solution discovered:

                       Removing the validation of "(Time of Day, Allow Override)" on the field allows it to accept 5pm, 5 pm, 5:00pm etc as times.  Now I think it'll be easy to just add on a script that checks for an "m" at the end of the string and adds it if it isn't there to allow for the "5p" input eventuality (5p will through the same error even without the validation setting).

                       Is there any harm in removing the Validation setting from the field?  It's set to be a time field, not text, so should be storing the data properly regardless.  Any thoughts on if I might run into problems down the road?

                  • 6. Re: Auto Formatting Time with script trigger
                    philmodjunk

                         I see no reason for the validation for a field of type time. If you enter an invalid entry such as "apple" in the field, FileMaker will still throw up an error message without that validation option.