Sunday, April 03, 2005

It's Coldfusion Time

Can someone tell me how to convert a number field in the output of a database from standard number to regular formatted time?

My database data looks like this: 1715 and I want it to output this: 5:15 PM

<cfset time="1715">
<cfset hours="1200">
<cfif time gt 1200>
<cfset newtime=time-hours>
<cfelse>
<cfset newtime=time>
</cfif>
And then put in the colon, space and PM.

OR

You just need to insert the colon in the proper place

<cfset myTime = timeFormat((Left(query.columnName, 2) & ':' & Right(query.columnName, 2)), 'hh:mm tt')>

This will work fine so long as the number is 4 characters long, if not you will have to adjust for length.

if time is 17:01:08

<cfset myTime = timeformat(query.columnName, "hh:mm:ss tt")>
this will output 05:01:08 PM

<cfset myTime = timeformat(query.columnName, "h:mm:ss tt")>
this will output 5:01:08 PM

<cfset myTime = timeformat(query.columnName, "HH:mm:ss")>
this will output 17:01:08

Masking characters that determine the format:

h: Hours; no leading zero for single-digit hours (12-hour clock)
hh: Hours; leading zero for single-digit hours (12-hour clock)
H: Hours; no leading zero for single-digit hours (24-hour clock)
HH: Hours; leading zero for single-digit hours (24-hour clock)
m: Minutes; no leading zero for single-digit minutes
mm: Minutes; a leading zero for single-digit minutes
s: Seconds; no leading zero for single-digit seconds
ss: Seconds; leading zero for single-digit seconds
l or L: Milliseconds. l gives 3 digits. L gives 2 digits.
t: One-character time marker string, such as A or P
tt: Multiple-character time marker string, such as AM or PM
short: equivalent to h:mm tt
medium: equivalent to h:mm:ss tt
long: medium followed by three-letter time zone; as in, 2:34:55 PM EST
full: same as long

0 Comments:

Post a Comment

<< Home