User Guide Cancel

DateTimeFormat

 

Description

Formats date and time values using date and time formatting conventions.

Returns

A string representing a formatted date and time value.

History

ColdFusion (2016 release) Update 3: SimpleDateFormat's mask a is no longer followed. Use the masks t and tt to return the character string A/P or AM/PM respectively.

ColdFusion 10: Added this function.

Syntax

dateTimeFormat (date)
dateTimeFormat (date [, mask])
dateTimeFormat (date [, mask, timeZone])
dateTimeFormat (date) dateTimeFormat (date [, mask]) dateTimeFormat (date [, mask, timeZone])
dateTimeFormat (date)
dateTimeFormat (date [, mask])
dateTimeFormat (date [, mask, timeZone])

Properties

Parameter

Description

date

Required. A date/time object, in the range 100 AD-9999 AD.

mask

Optional. 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.
  •     EEE: Day of the week as a three-letter abbreviation.
  •     EEEE: 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.
  •     M: Month in  year .
  •     D: Day in  year .    yy : Year as last two digits; leading zero for years less than 10.
  •     yyyy : Year represented by four digits.
  •     YYYY: Week year represented by four digits.
  •     Y: Week year.
  •     YY: Week year as last two digits; leading zero for years less than 10.
  •     G: Period/era string. (e.g. BC, AD)
  •     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)
  •     n: minutes; no leading zero for single-digit minutes
  •     nn : 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, with no leading zeros
  •     t: one-character time marker string, such as A or P
  •     tt: multiple-character time marker string, such as AM or PM
  •     w: Week of the year as digit .
  •     ww : Week of the year as digits. Leading zero for single-digit week.
  •     W: Week of the month as digit .
  •     WW: Week of the month as digits. Leading zero for single-digit week.

The following masks tell how to format the full date and time and cannot be combined with other masks:

  • short: equivalent to m/d/y h: nn tt
  • medium: equivalent to mmm d, yyyy   h: nn :ss tt
  • long: medium with full month name rather than abbreviation , followed by three-letter time zone; as in, mmmm d, yyyy h:mm :ss tt EST
  • full: equivalent to EEEE, mmmm d, yyyy h:nn :ss tt EST
  • iso: Formats the date time in ISO8601 format

The function also follows Java date time mask, except for mask a. For more information, refer to Date and Time Patterns topic in SimpleDateFormat Java API page.

JDK7 and JDK8 introduces the masks w, ww , W, and WW.

timeZone

The time-zone information. You can specify in either of the following formats:

  • Abbreviation such as GMT or PST
  • Full name such as Europe/Dublin
    By default, this is the time-zone followed by the system.

Example

<cfscript>
Date1 = "{ts '2018-11-05 12:13:50'}";
DateTimeFormat= DateTimeFormat(Date1,"m")
DateTimeFormat1= DateTimeFormat(Date1,"mm")
DateTimeFormat2= DateTimeFormat(Date1,"mmm")
DateTimeFormat3= DateTimeFormat(Date1,"mmmm")
DateTimeFormat4= DateTimeFormat(Date1,"M")
writeOutput("Month as digits; no leading zero for single-digit months: " & DateTimeFormat & "<br/>")
writeOutput("Month as digits; leading zero for single-digit months: " & DateTimeFormat1 & "<br/>")
writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat2 & "<br/>")
writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat3 & "<br/>")
writeOutput(" Month in year: " & DateTimeFormat4 & "<br/>")
</cfscript>
<cfscript> Date1 = "{ts '2018-11-05 12:13:50'}"; DateTimeFormat= DateTimeFormat(Date1,"m") DateTimeFormat1= DateTimeFormat(Date1,"mm") DateTimeFormat2= DateTimeFormat(Date1,"mmm") DateTimeFormat3= DateTimeFormat(Date1,"mmmm") DateTimeFormat4= DateTimeFormat(Date1,"M") writeOutput("Month as digits; no leading zero for single-digit months: " & DateTimeFormat & "<br/>") writeOutput("Month as digits; leading zero for single-digit months: " & DateTimeFormat1 & "<br/>") writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat2 & "<br/>") writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat3 & "<br/>") writeOutput(" Month in year: " & DateTimeFormat4 & "<br/>") </cfscript>
<cfscript>
    Date1 = "{ts '2018-11-05 12:13:50'}";
    DateTimeFormat= DateTimeFormat(Date1,"m")
    DateTimeFormat1= DateTimeFormat(Date1,"mm")
    DateTimeFormat2= DateTimeFormat(Date1,"mmm")
    DateTimeFormat3= DateTimeFormat(Date1,"mmmm")
    DateTimeFormat4= DateTimeFormat(Date1,"M")
    writeOutput("Month as digits; no leading zero for single-digit months: " & DateTimeFormat & "<br/>")
    writeOutput("Month as digits; leading zero for single-digit months: " & DateTimeFormat1 & "<br/>")
    writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat2 & "<br/>")
    writeOutput(" Month as a three-letter abbreviation.: " & DateTimeFormat3 & "<br/>")
    writeOutput(" Month in year: " & DateTimeFormat4 & "<br/>")
</cfscript>

The code sample above produces the following output:

Month as digits; no leading zero for single-digit months: 11
Month as digits; leading zero for single-digit months: 11
Month as a three-letter abbreviation.: Nov
Month as a three-letter abbreviation.: November
Month in year: 11

Get help faster and easier

New user?