DateFormat() Example

TimeFormat() Example

Returns a formatted date/time value. If no mask is specified, DateFormat function returns date value using the dd-mmm-yy format.

Syntax
DateFormat(date [, mask ])

date
Date/time object in the period from 1601 AD to 9999 AD.

mask
Set of characters that are used to show how ColdFusion should display the date:

KEY for Dates:
d Day of the month as digits with no leading zero for single-digit days.
dd Day of the month as digits with a 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 digitswith no leading zero for single-digit months.
mm Month as digits with a 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 with no leading zero for years less than 10.
yy Year as last two digits with a leading zero for years less than 10.
yyyy Year represented by four digits.
gg Period/era string.
Currently ignored, but reserved for future use.

Usage
When passing a date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object, returning undesired results.

Note On UNIX, there is a switch that provides fast date-time parsing. If you have enabled this switch, you must refer to dates in expressions in the following order: month, day, and year. For example:

<CFIF "11/23/1998" GT "11/15/1998">

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

The Fast Date/Time Parsing switch is set on the ColdFusion Administrator Server Settings page. Please refer to Administering ColdFusion Server for more information about ColdFusion settings.

Examples

First, set a date/time variable:
•••<CFSET todayDate = Now()>
where #Now()# = {ts '2020-07-02 03:51:30'}

Today's date is {ts '2020-07-02 03:51:30'}. CODE: Today's date is {ts '2020-07-02 03:51:30'}.

Using DateFormat, we can display that date in a number of different ways:

02-Jul-20
•••DateFormat(todayDate)
Jul-02-2020
•••DateFormat(todayDate, "mmm-dd-yyyy")
July 2, 2020
•••DateFormat(todayDate, "mmmm d, yyyy")
07/02/2020
•••DateFormat(todayDate, "mm/dd/yyyy")
2-Jul-2020
•••DateFormat(todayDate, "d-mmm-yyyy")
Thu, July 02, 2020
•••DateFormat(todayDate, "ddd, mmmm dd, yyyy")
2/7/20
•••DateFormat(todayDate, "d/m/yy")

 

 

 

Returns a custom-formatted time value. If no mask is specified, the TimeFormat function returns time value using the hh:mm tt format.

Syntax
TimeFormat(time [, mask ])

time
Any date/time value or string convertible to a time value.

mask
A set of masking characters determining the format:

KEY for Time:
h Hours with no leading zero for single-digit hours. (Uses a 12-hour clock.)
hh Hours with a leading zero for single-digit hours. (Uses a 12-hour clock.)
H Hours with no leading zero for single-digit hours. (Uses a 24-hour clock.)
HH Hours with a leading zero for single-digit hours. (Uses a 24-hour clock.)
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 time marker string, such as A or P
tt Multiple-character time marker string, such as AM or PM

Usage
When passing a date/time value as a string, make sure it is enclosed in quotes. Otherwise, it is interpreted as a number representation of a date/time object, returning undesired results.

Examples
First, set a date/time variable:
•••<CFSET todayDate = #Now()#> where #Now()# = {ts '2020-07-02 03:51:30'}

Using TimeFormat, we can display that date/time value
in a number of different ways:

03:51 AM
•••#TimeFormat(todayDate)#
03:51:30
•••#TimeFormat(todayDate, "hh:mm:ss")#
03:51:30A
•••#TimeFormat(todayDate, "hh:mm:sst")#
03:51:30AM
•••#TimeFormat(todayDate, "hh:mm:sstt")#
03:51:30
•••#TimeFormat(todayDate, "HH:mm:ss")#