Not yet rated

Problem

You need to format a date/time object.

Solution

Use dateFormat(), LSDateFormat(), timeFormat() or LSTimeFormat().

Detailed explanation

To format a date for the English (US) locale, use the dateFormat() function. For times using the U.S. format, use timeFormat(). To format a date using the conventions of a different locale, use the LSDateFormat() function. Locale specific time formatting can be accomplished using LSTimeFormat().

Very few situations call for formatting your dates and times like this:

{ts '2002-04-01 21:51:50'}

More often than not, you'll want or need to format a date/time object in a way different from how ColdFusion or your database stores the value internally. This is easily handled using the dateFormat() function:

dateFormat(date [, mask])

DateFormat() returns the specified date formatted according to the mask. If no value is specified for mask, DateFormat() uses the default dd-mmm-yy. Valid entries for mask are:

d       Day of the month as a number with no leading zero for
single-digit days.
dd      Day of the month as a number with a leading zero for
single-digit days.
ddd     Three-letter abbreviation for day of the week
dddd    Full name of the day of the week.
m       Month as a number with no leading zero for single-digit
months.
mm      Month as a number with a leading zero for single-digit
months.
mmm     Three-letter abbreviation for the month.
mmmm    Full name of the month.
y       Last two digits of year with no leading zero for years less
than 10.
yy      Last two digits of year with a leading zero for years less
than 10.
yyyy    Four digit year.
gg      Period/era
short   Java short date format
medium  Java medium date format
long    Java lomg date format
full    Java full date format

There is a wide variety of ways you can format your dates using DateFormat(). Here are some examples:

<cfset thedate = now()>

<cfoutput>
TheDate = #DateFormat(TheDate)#
<p>
m/d/yy: #DateFormat(TheDate, 'm/d/yy')#<br>
mm/dd/yy: #DateFormat(TheDate, 'mm/dd/yy')#<br>
mm/dd/yyyy: #DateFormat(TheDate, 'mm/dd/yyyy')#<br>
dd/mm/yyyy: #DateFormat(TheDate, 'dd/mm/yyyy')#<br>
dd mmm yy: #DateFormat(TheDate, 'dd mmm yy')#<br>
dddd mmmm dd, yyyy: #DateFormat(TheDate, 'dddd mmmm dd, yyyy')#
</cfoutput>

You should note that dateFormat() supports U.S. date formats only. To use a locale specific date format, use the LSDateFormat() function. LSDateFormat() returns a locale specific date format according to the mask you provide. If no mask is specified, LSDateFormat() uses the locale specific default. This can vary depending on the locale your server is set to. Valid date masks are the same as for the dateFormat() function.

ColdFusion does not currently have a combined function for formatting dates and times. If you need to format the time portion of a date/time object, you''ll need to use the timeFormat() function:

timeFormat(time [, mask])

TimeFormat() returns the time formatted according to the mask you provide. If no value is specified for mask, timeFormat() uses the default hh:mm tt. Valid entries for mask are:

h       Hours based on a 12-hour clock with no leading zeros for
single-digit hours
hh      Hours based on a 12-hour clock with leading zeros for
single-digit hours
H       Hours based on a 24-hour clock with no leading zeros for
single-digit hours
HH      Hours based on a 24-hour clock with leading zeros for
single-digit hours
m       Minutes with no leading zero for single-digit minutes
mm      Minutes with a leading zero for single-digit minutes
s       Seconds with no leading zero for single-digit seconds
ss      Seconds with a leading zero for single-digit seconds
t       Single character meridian, either A or P
tt      Multi character meridian, either AM or PM
short   Java short time format
medium  Java medium time format
long    Java lomg time format
full    Java full time format

Examples:

<cfset thetime = now()>

<cfoutput>
TheTime = #timeFormat(TheTime)#
<p>
TimeFormat(TheTime, 'h:m:s'): #timeFormat(TheTime,
'h:m:s')#<br>
TimeFormat(TheTime, 'h:m:s t'): #timeFormat(TheTime, 'h:m:s
t')#<br>
TimeFormat(TheTime, 'hh:mm:ss'): #timeFormat(TheTime,
'hh:mm:ss')#<br>
TimeFormat(TheTime, 'hh:mm:ss tt'): #timeFormat(TheTime, 'hh:mm:ss
tt')#<br>
TimeFormat(TheTime, 'H:M:ss'): #timeFormat(TheTime,
'H:M:s')#<br>
TimeFormat(TheTime, 'HH:MM:ss'): #timeFormat(TheTime,
'HH:MM:ss')#<br>
</cfoutput>

Locale specific times are formatted using LSTimeFormat(). This works identically to the timeFormat() function, and uses the same masks. If no mask is provided, the function reverts to the locale specific default.


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes