Code - Custom Function

timeFormat ( thetime ; leader )

BrianDunning.com, Brian Dunning http://www.briandunning.com/filemaker-custom-functions/list.php


timeFormat ( thetime ; leader )
Rate this function: Average rating: 0.0 (0 votes) Discuss this Custom FunctionBob Patin, Longterm Solutions LLC
http://www.longtermsolutions.com
Formats lots of shorthand text strings as time
==================== SAMPLE INPUT AND OUTPUT ====================
INPUT
timeFormat("1p" ; 1 )
timeFormat("123" ; 0 )
timeFormat("4p" ; 0 )
timeFormat("3" ; 1 )
timeFormat("3" ; 0 )
OUTPUT
01:00 PM
1:23 AM
4:00 PM
03:00 AM
3:00 AM
==================== DESCRIPTION ====================
timeFormat will take various text input and format it as time. For example, you can enter any of these: 1, 1a, 9p, 1230, 1230p, 125a, 125p.
Error-checking will reject any extraneous characters and show an error message for input that can't be interpreted as time.
==================== CUSTOM FUNCTION ====================

/* timeFormat



Written by Bob Patin

Longterm Solutions LLC

bob@longtermsolutions.com

http://www.longtermsolutions.com



PARAMETERS:

thetime - a text string which can include

9

9a

9:00a

9:00 (resolves to 9:00 AM)

930

1230

any string more than 5 characters will cause an INPUT ERROR message





leader -- 1 puts a leading zero before the hour if it's a single-digit hour



*/



Let ( [



thetime = Filter(Lower ( thetime ); "amp1234567890:");

nums = Filter(thetime ; "0123456789:");

len = Length(nums);



// set AM or PM

// UPPER CASE

ampm =

Case(

Position ( thetime ; "p" ; 1 ; 1 ) >0; "PM";

Position ( thetime ; "a" ; 1 ; 1 ) > 0; "AM";

"AM"

);



// check for the colon

nums = If (Position(nums ; ":" ; 1 ; 1) > 1;

nums;

Case (

len > 4 ; "13:13";

len = 4; Left(nums ; 2) & ":" & Right(nums ; 2);

len = 3 and leader; "0" & Left(nums ; 1) & ":" & Right(nums ; 2);

len = 3 ; Left(nums ; 1) & ":" & Right(nums ; 2);

len = 2 ; nums & ":00";

len = 1 and leader; "0" & nums & ":00";

len = 1 ; nums & ":00"

));



// get the HOURS

h = Case (leader ; Left(nums ; 2);

(leader = 0) and (Position(nums ; ":" ; 1 ; 1 ) = 3) ; Left(nums ; 2);

Left(nums ; 1)

);



// get the MINUTES

m = Right(nums ; 2);



// ----- CHECK FOR INPUT ERROR

thetime = If (GetAsNumber(h) > 12 or GetAsNumber(m) > 59;

"INPUT ERROR";

h & ":" & m & " " & ampm

)

// --------------



] ;



thetime



)


Referring URL: 'http://www.briandunning.com/cf/1124'

Attached File:    

Screenshot:    


Keywords: ''

ClipManager: