dateTimeFormat

Formats a datetime value using U.S. date and time formats. For international date support, use lsDateTimeFormat.

dateTimeFormat(date [, mask [, timezone]]) → returns string

Member Function Syntax

datetime.dateTimeFormat([mask [, timezone]])

Argument Reference

date date
Required

The datetime object (100AD-9999AD).
NOTE: This parameter is named datetime in Lucee.

mask string
Default: dd-mmm-yyyy HH:nn:ss

The mask used to format the datetime output.
- 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. (Dec)
- mmmm : Month as its full name.
- M : Month in year. (pre-CF2016u3)
- D : Day in year. (pre-CF2016u3)
- 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. (pre-CF2016u3)
- Y : Week year. (pre-CF2016u3)
- YY : Week Year as last two digits; leading zero for years less than 10. (pre-CF2016u3)
- 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; 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. (JDK7+)
- ww : Week of the year as digits; leading zero for single-digit week. (JDK7+)
- W : Week of the month as digit. (JDK7+)
- WW : Week of the month as digits; leading zero for single-digit week. (JDK7+)

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:mm:ss tt EST"
- iso CF 2016+ Lucee 5.3.8+ Formats the date time in ISO8601 format
- iso8601 Lucee 4.5+ 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 string
Default: System time-zone

The timezone to use. Can be the 3 letter code ("EST","UTC") or the full name full {"America/New_York")

Compatibility

ColdFusion:

Version 10+ Member function is available in CF11+.

Lucee:

Version 4.5+ The timezone argument does not appear to convert the date from the system timezone to the specified timezone as it does in ACF. Member function is available in Lucee5+.

Railo:

Version 4.0.1+ No documentation exists for this function on Railo, however LSDateTimeFormat is documented. Follows Java date time mask. For details, see the section Date and Time Patterns at the following URL: http://docs.oracle.com/javase/1.4.2/docs/api/java/text/SimpleDateFormat.html

OpenBD:

Some of the mask tokens differ from Adobe ColdFusion

Links more information about dateTimeFormat

Examples
Sample code invoking the dateTimeFormat function

On Lucee with Java 11 adds a comma after year bug: LDEV-3744

dateTimeFormat("2015-04-11 19:02", "short")

Expected Result: 4/11/15 7:02 PM

On Lucee with Java 11 adds a comma after year bug: LDEV-3744

dateTimeFormat("2015-04-11 19:02", "medium")

Expected Result: Apr 11, 2015 7:02:00 PM

On Lucee with Java 11 adds at before the time bug: LDEV-3744

dateTimeFormat("2015-04-11 19:02", "long")

Expected Result: April 11, 2015 7:02:00 PM UTC

On Lucee with Java 11 adds at before the time bug: LDEV-3744

dateTimeFormat("2015-04-11 19:02", "full")

Expected Result: Saturday, April 11, 2015 7:02:00 PM UTC

Uses the CF 2016+ iso or the Lucee 4.5+ iso8601 format depending on the engine. Note the depending on Java version the timezone format may differ (on Lucee at least, possible due to LDEV-3744)

dateTimeFormat("2015-04-11 19:02", (server.keyExists("lucee")) ? "iso8601" : "iso")

Expected Result: 2015-04-11T19:02:00+0000

Simple date/time formatting using the member function syntax

createDateTime( 2022, 10, 1, 9, 30, 0 ).dateTimeFormat( 'mm/dd/yyyy hh:nn:ss tt' )

Expected Result: 10/01/2022 09:30:00 AM

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub