You need to format a date/time object.

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().

Few situation calls 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 date formatted according to 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>

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.

Why ColdFusion doesn't have a combined function for formatting dates and times is beyond me. 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 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 question was written by Rob Brooks-Bilson
It was last updated on January 6, 2006.

Categories

Dates/Times

Comments

comments powered by Disqus