View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > TimeFormat PreviousNext

TimeFormat

Formats a time value using US English time formatting conventions.

A custom-formatted time value. If no mask is specified, returns a time value using the hh:mm tt format. For international time formatting, see LSTimeFormat.

Date and time functions, Display and formatting functions

TimeFormat(time [, mask ])

CreateTime, Now, ParseDateTime, LSTimeFormat, DateFormat

ColdFusion MX 6.1: Added the mask character L or l to represent milliseconds.

ColdFusion MX:

Parameter

Description

time

A date/time value or string to convert

mask

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: Milliseconds
  • 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

When passing a date/time value as a string, you must enclose it in quotation marks. Otherwise, it is interpreted as a number representation of a date/time object.

Database query results for date and time values can vary in sequence and formatting unless you use functions to format the results. To ensure that dates and times display with appropriate formatting, and that users of your ColdFusion application are not confused by dates and times displayed, Macromedia recommends that you use the DateFormat and TimeFormat functions to format date and time values from queries. For more information and examples, see TechNote 22183, "ColdFusion Server (5 and 4.5.x) with Oracle: Formatting Date and Time Query Results," at www.coldfusion.com/Support/KnowledgeBase/SearchForm.cfm.

<cfset todayDate = #Now()#>
<body>
<h3>TimeFormat Example</h3>
<p>Today's date is <cfoutput>#todayDate#</cfoutput>.
<p>Using Timeformat, we can display the value in different ways:
<cfoutput>
<ul>
   <li>#TimeFormat(todayDate)#
   <li>#TimeFormat(todayDate, "hh:mm:ss")#
   <li>#TimeFormat(todayDate, "hh:mm:sst")#
   <li>#TimeFormat(todayDate, "hh:mm:sstt")#
   <li>#TimeFormat(todayDate, "HH:mm:ss")#
</ul>
</cfoutput>   
</body>

Contents > CFML Reference > ColdFusion Functions > TimeFormat PreviousNext

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


spage said on Aug 25, 2004 at 8:39 PM :
You can't generate a standard ISO 8601 W3C Date and Time string like 1997-07-16T19:20;
If you use yyyy-MM-ddTHH:mm:ss the 'T' turns into A for am or P for pm, and there's no way to escape or quote a 'T' in the middle of a string as far as I can tell. :-(
halL said on Aug 26, 2004 at 12:18 PM :
Spage is correct in that the format he shows will not work for the desired output. Also, he is using an undocumented and unsupported characteristic of TimeFormat, the ability to specify a date mask. (You cannot do a similar thing with DateFormat). The recommended way to get the desired string is by concatenating a DateFormat function, the character T, and a TimeFormat function, as in the following lines:

as a variable:
<cfset foo=dateformat(now(), "yyyy-mm-dd") & "T" & TimeFormat(now(), "HH:mm:ss")>

for output:
<cfoutput>#dateformat(now(), "yyyy-mm-dd")#T#TimeFormat(now(), "HH:mm:ss")#</cfoutput>
Damien@Limu said on Sep 22, 2004 at 10:15 AM :
l - two digit milisecond display.
L - two digit milisecond display.

That caught me up when I was inserting into a fixed-width display and used "L" instead of "l".

Damien
Damien@Limu said on Sep 22, 2004 at 10:15 AM :
Should have been...

l - two digit milisecond display.
L - three digit milisecond display.

Sorry.
blricks said on Jan 10, 2005 at 9:14 AM :
Is there a way to either display the current user's clock time or to add and subtract hours from the server's time? For example, I live in Arizona with no DST, it would be prefered to display Arizona Standard Time without having the server reside in AZ.

Currently, the server resides in MN, which is currently 1 hour ahead of me, but does have DST. The only other option tis to display current user time, but I don't know how to do any of these.
No screen name said on Feb 2, 2005 at 7:39 PM :
function getTime () {
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var temp = ""+((hour>12) ? hour-12 : hour);
temp += ((minute<10) ? ":0" : ":")+minute;
temp += ((second<10) ? ":0" : ":")+second;
temp += (hour>=12) ? " P.M." : " A.M.";

return temp;
}


Place an instance of this new symbol on the main Timeline of the movie.
Select the new movie clip instance, and open the Actions panel (Window > Actions) if it's not already open.
Enter the following script into the Actions panel: onClipEvent (enterFrame) {
time = getTime();
}

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/funca107.htm