cf.Objective

Speaking at cf.Objective

Twitter

Twitter is resting...

Recommended Books

Blog

New function - getMeridiem

I just submitted a function to CFLib.org and thought I'd post it here too. getMeridiem() isn't super fancy, it just returns the "AM/PM" portion of a datetime (the name of which is the "meridiem"). As you can see, you can get that information from the built-in function TimeFormat(). All I'm doing is adding a little extra whitespace housecleaning and validation (which is useful if the dates might be badly formed...a situation I ran into last week...hence my writing this function). Also, I find getMeridiem() is a more intuitive name and it's more clear what that function does than TimeFormat( theTime, "tt" )...self documenting code and all that.

The source code and examples look like so:

view plain print about
1<cffunction name="getMeridiem" returntype="string" output="false">
2    <cfargument name="timePassedIn" type="string" required="true" />
3
4 <cfset var meridiem = "" />
5
6 <cfset arguments.timePassedIn = Trim( arguments.timePassedIn ) />
7 <cfif not IsDate( arguments.timePassedIn )>
8     <cfreturn meridiem />
9 <cfelse>
10     <cfset meridiem = TimeFormat( arguments.timePassedIn, "tt" ) />
11 </cfif>
12
13 <cfreturn meridiem />
14
15</cffunction>

Examples:

<cfoutput>
#getMeridiem( "4/2/2012 14:33:00" )#
#getMeridiem( "14:33:00" )#
#getMeridiem( CreateTime( 17, 30, 00 ) )#
</cfoutput>

Feel free to add this to your own Date/Time libraries for later use. Feedback is welcome.

-nolan

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hi Nolan
Thanks for submitting this UDF. Unfortunately I've just rejected it :-(

Here's why. You've specified the argument as a DATE, so all the trim() and isDate() stuff is redundant (the value won't get that far if it's not already a date), leaving just the timeFormat() bit. And it's more work for people to grab your UDF, include it in their code and call it than just to use timeFormat(). This is especially so because it's always gonna be better to use a built-in function than a UDF that - for all intents and purposes - does the same as the built-in function.

I hope you don't find this discouraging: CFLib certainly does appreciate that you thought to submit this, and hope you continue to submit your UDFs.

Cheers / sorry.

--
Adam
# Posted By Adam Cameron | 11/2/12 9:28 PM

Search

Recent Comments

CFObjective Notes - Mobile Debugging Tips and Tricks, Ray Camden
Raymond Camden said: That makes perfect sense. Ok, I'll try to add that. Thanks! [More]

CFObjective Notes - Mobile Debugging Tips and Tricks, Ray Camden
Nolan Erck said: Hi Ray, The only thing I can think of is (and this is just a... [More]

CFObjective Notes: Errors Are Best When Emailed Said Nobody Ever - Adam Tuttle
Nolan Erck said: No idea, not much time was spent discussing Hoth. Most of th... [More]

CFObjective Notes - Mobile Debugging Tips and Tricks, Ray Camden
Raymond Camden said: That makes perfect sense. Ok, I'll try to add that. Thanks! [More]

CFObjective Notes - Mobile Debugging Tips and Tricks, Ray Camden
Nolan Erck said: Hi Ray, The only thing I can think of is (and this is just a... [More]

CFObjective Notes: Errors Are Best When Emailed Said Nobody Ever - Adam Tuttle
Nolan Erck said: No idea, not much time was spent discussing Hoth. Most of th... [More]



BlogCFC was created by Raymond Camden. This blog is running version 5.9.7. Contact Blog Owner