DateFormat

Description

Formats a date value. Supports dates in the U.S. date format. For international date support, use LSDateFormat.

Return value

A formatted date/time object. If no mask is specified, returns the value in dd-mmm-yy format.

Category

Date and time functions

Syntax

DateFormat("date" [, "mask" ]) 

See also

Now, CreateDate, LSDateFormat, LSParseDateTime, LSTimeFormat, TimeFormat, ParseDateTime

History

New in ColdFusion MX: This function supports the short, medium, long, and full mask attribute options.

Parameters

Parameter Description
date
Date/time object, in the range 100 AD-9999 AD. See "How ColdFusion processes two-digit year values".
mask
Characters that show how ColdFusion displays a date:
  • d: Day of the month as digits; no leading zero for single-digit days.
  • dd: Day of the month as digits; leading zero for single-digit days.
  • ddd: Day of the week as a three-letter abbreviation.
  • dddd: Day of the week as its full name.
  • m: Month as digits; no leading zero for single-digit months.
  • mm: Month as digits; leading zero for single-digit months.
  • mmm: Month as a three-letter abbreviation.
  • mmmm: Month as its full name.
  • y: Year as last two digits; no leading zero for years less than 10.
  • yy: Year as last two digits; leading zero for years less than 10.
  • yyyy: Year represented by four digits.
  • gg: Period/era string. Ignored. Reserved.
  • short
  • medium
  • long
  • full

Usage

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

Note:   You can pass the CreateDate function or Now function as the date parameter of this function; for example: #DateFormat(CreateDate(2001, 3, 3))#

If the switch is set, the default date format returned by this function cannot be parsed in an expression. However, if you specify a mask to indicate order (such as mm/dd/yyyy), the date returned by this function can be parsed.

Date and time values in database query results can vary in sequence and formatting unless you use functions to format them. To ensure that application users correctly understand displayed dates and times, Macromedia recommends that you use this function and the LSDateFormat, TimeFormat, and LSTimeFormat functions to format resultset values. For more information and examples, see TechNote 22183, "ColdFusion Server (5 and 4.5.x) with Oracle: Formatting Date and Time Query Results," on our Web site at http://www.coldfusion.com/Support/KnowledgeBase/SearchForm.cfm.

Example

<cfset todayDate = Now()>
<body>
<h3>DateFormat Example</h3>
<p>Today's date is <cfoutput>#todayDate#</cfoutput>.
<p>Using DateFormat, we can display that date in different ways:
<cfoutput>
<ul>
  <li>#DateFormat(todayDate)#
  <li>#DateFormat(todayDate, "mmm-dd-yyyy")#
  <li>#DateFormat(todayDate, "mmmm d, yyyy")#
  <li>#DateFormat(todayDate, "mm/dd/yyyy")#
  <li>#DateFormat(todayDate, "d-mmm-yyyy")#  
  <li>#DateFormat(todayDate, "ddd, mmmm dd, yyyy")#  
  <li>#DateFormat(todayDate, "d/m/yy")#
</ul>  
</cfoutput>  

Comments