Getting started with Dates in ColdFusion

Readability

Getting started with Dates in ColdFusion

The fol­low­ing exam­ples show how you can get started with cre­at­ing and for­mat­ting date val­ues, as well as many other date related func­tions in ColdFusion.

Get­ting the cur­rent date/​time in ColdFusion

To get the cur­rent date/​time on the Cold­Fu­sion server you can use the now() func­tion, as seen in the fol­low­ing example:

#now()#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

{ts ‘20101112 22:34:52′}

Note: If you want to get the date/​time of the user’s com­puter, you’d need to use a client side lan­guage, such as JavaScript.

Cre­at­ing dates and times in ColdFusion

If you want to cre­ate a spe­cific date or date/​time, you can use the createDate() and/​or createDateTime() func­tions, as seen in the fol­low­ing example:






createDate(): #dateObj#
createTime(): #timeObj#
createDateTime(): #dateTimeObj#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

cre­ate­Date(): {ts ‘20100904 00:00:00′}
cre­ate­Time(): {ts ‘18991230 22:43:15′}
cre­ate­Date­Time(): {ts ‘20100904 22:43:15′}

You can also cre­ate ODBC com­pli­ant date and/​or date time objects in Cold­Fu­sion using the createODBCDate() and func­tions, as seen in the fol­low­ing example:








createDate(): #dateObj#
createDateTime(): #dateTimeObj#
createODBCDate(): #odbcDateObj#
createODBCTime(): #odbcTimeObj#
createODBCDateTime(): #odbcDateTimeObj#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

cre­ate­Date(): {ts ‘20100904 00:00:00′}
cre­ate­Date­Time(): {ts ‘20100904 22:43:15′}
cre­ateOD­BC­Date(): {d ‘201009-​04′}
cre­ateOD­BC­Time(): {t ’22:43:15′}
cre­ateOD­BC­Date­Time(): {ts ‘20100904 22:43:15′}

Pars­ing dates/​times in ColdFusion

The fol­low­ing exam­ple shows how you can parse a date/​time from a string using the parseDateTime() function:




    
value date
#dateTimeObj# #parseDateTime(dateTimeObj)#
April 30, 1977 #parseDateTime("April 30, 1977")#
Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (pop) #parseDateTime("Sat, 4 Sep 2010 21:43:15 -0800 (PDT)", "pop")#
Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (standard) #parseDateTime("Sat, 4 Sep 2010 21:43:15 -0800 (PDT)", "standard")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

value date
{ts ‘20100904 22:43:15′} {ts ‘20100904 22:43:15′}
April 30, 1977 {ts ‘19770430 00:00:00′}
Sat, 4 Sep 2010 21:43:150800 (PDT) (pop) {ts ‘20100905 05:43:15′}
Sat, 4 Sep 2010 21:43:150800 (PDT) (standard) {ts ‘20100904 21:43:15′}

Note: Cold­Fu­sion will gen­er­ate an error if you pass an invalid date (such as “April 32, 1977″).

Deter­min­ing if a vari­able or string is a valid date in ColdFusion

The fol­low­ing exam­ple shows how you can deter­mine if a date is valid by using the isDate() function:




    
value is date
#dateTimeObj# #isDate(dateTimeObj)#
17 #isDate(17)#
April 30, 1977 #isDate("April 30, 1977")#
April 31, 1977 #isDate("April 31, 1977")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

value is date
{ts ‘20100904 22:43:15′} YES
17 NO
April 30, 1977 YES
April 31, 1977 NO

Adding units of time to a date

To add units of time (years, quar­ters, months, weeks, hours, etc) to a date, you can use the dateAdd() func­tion, as seen in the fol­low­ing example:




    
original date #dateObj#
date part output
yyyy (year) #dateAdd("yyyy", 1, dateObj)#
q (quarter) #dateAdd("q", 1, dateObj)#
m (month) #dateAdd("m", 1, dateObj)#
y (day of year) #dateAdd("y", 1, dateObj)#
d (day) #dateAdd("d", 1, dateObj)#
w (weekday) #dateAdd("w", 1, dateObj)#
ww (week) #dateAdd("ww", 1, dateObj)#
h (hour) #dateAdd("h", 1, dateObj)#
n (minute) #dateAdd("n", 1, dateObj)#
s (second) #dateAdd("s", 1, dateObj)#
l (millisecond) #dateAdd("l", 1, dateObj)#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

orig­i­nal date {ts ‘20000101 00:00:00′}
date part out­put
yyyy (year) {ts ‘20010101 00:00:00′}
q (quar­ter) {ts ‘20000401 00:00:00′}
m (month) {ts ‘20000201 00:00:00′}
y (day of year) {ts ‘20000102 00:00:00′}
d (day) {ts ‘20000102 00:00:00′}
w (week­day) {ts ‘20000103 00:00:00′}
ww (week) {ts ‘20000108 00:00:00′}
h (hour) {ts ‘20000101 01:00:00′}
n (minute) {ts ‘20000101 00:01:00′}
s (sec­ond) {ts ‘20000101 00:00:01′}
l (mil­lisec­ond) {ts ‘20000101 00:00:00′}

For­mat­ting dates in ColdFusion

To for­mat a date value, you can use the dateFormat() func­tion, as seen in the fol­low­ing example:

#dateFormat(now())#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

12-​Nov-​10

As you can see, the default date for­mat is DD-​MMM-​YY. You can also pass an optional sec­ond para­me­ter to the dateFormat() func­tion which rep­re­sents a “mask” that con­trols how the date will be shown.

#dateFormat(now(), "MMM D, YYYY")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

Nov 12, 2010

There are sev­eral dif­fer­ent for­mat strings you can use (as well as a few pre­de­fined for­mats such as “short”, “medium”, “long”, and “full”):


 

    
mask output
"d" (day) #dateFormat(dat, "d")#
"dd" (day) #dateFormat(dat, "dd")#
"ddd" (day abbrev.) #dateFormat(dat, "ddd")#
"dddd" (day full) #dateFormat(dat, "dddd")#
"m" (month) #dateFormat(dat, "m")#
"mm" (month) #dateFormat(dat, "mm")#
"mmm" (month abbrev.) #dateFormat(dat, "mmm")#
"mmmm" (month full) #dateFormat(dat, "mmmm")#
"yy" (year) #dateFormat(dat, "yy")#
"yyyy" (year) #dateFormat(dat, "yyyy")#
"gg" ("AD") #dateFormat(dat, "gg")#
"short" (m/d/y) #dateFormat(dat, "short")#
"medium" (mmm d, yyyy) #dateFormat(dat, "medium")#
"long" (mmmm d, yyyy) #dateFormat(dat, "long")#
"full" (dddd, mmmm d, yyyy) #dateFormat(dat, "full")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

mask out­put
“d” (day) 4
“dd” (day) 04
“ddd” (day abbrev.) Sat
“dddd” (day full) Sat­ur­day
“m” (month) 9
“mm” (month) 09
“mmm” (month abbrev.) Sep
“mmmm” (month full) Sep­tem­ber
“yy” (year) 10
“yyyy” (year) 2010
“gg” (“AD”) AD
“short” (m/​d/​y) 9÷4÷10
“medium” (mmm d, yyyy) Sep 4, 2010
“long” (mmmm d, yyyy) Sep­tem­ber 4, 2010
“full” (dddd, mmmm d, yyyy) Sat­ur­day, Sep­tem­ber 4, 2010

For­mat­ting times in ColdFusion

Sim­i­larly, you can for­mat times using the timeFormat() func­tion, as seen in the fol­low­ing example:




    
mask output
"h" (hours/12h) #timeFormat(dateTimeObj, "h")#
"hh" (hours/12h) #timeFormat(dateTimeObj, "hh")#
"H" (hours/24h) #timeFormat(dateTimeObj, "H")#
"HH" (hours/24h) #timeFormat(dateTimeObj, "HH")#
"m" (minutes) #timeFormat(dateTimeObj, "m")#
"mm" (minutes) #timeFormat(dateTimeObj, "mm")#
"s" (minutes) #timeFormat(dateTimeObj, "s")#
"ss" (minutes) #timeFormat(dateTimeObj, "ss")#
"l" (milliseconds) #timeFormat(dateTimeObj, "l")#
"t" (A/P) #timeFormat(dateTimeObj, "t")#
"tt" (AM/PM) #timeFormat(dateTimeObj, "tt")#
"short" (h:mm tt) #timeFormat(dateTimeObj, "short")#
"medium" (h:mm:ss tt) #timeFormat(dateTimeObj, "medium")#
"long" #timeFormat(dateTimeObj, "long")#
"full" #timeFormat(dateTimeObj, "full")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

mask out­put
“h” (hours/​12h) 9
“hh” (hours/​12h) 09
“H” (hours/​24h) 21
HH” (hours/​24h) 21
“m” (min­utes) 43
“mm” (min­utes) 43
“s” (min­utes) 15
“ss” (min­utes) 15
“l” (mil­lisec­onds) 0
“t” (A/​P) P
“tt” (AM/​PM) PM
“short” (h:mm tt) 9:43 PM
“medium” (h:mm:ss tt) 9:43:15 PM
“long” 9:43:15 PM PDT
“full” 9:43:15 PM PDT

Get­ting indi­vid­ual date parts in ColdFusion

You can also get indi­vid­ual date/​time parts using spe­cific func­tions such as year(), month(), hour(), second(), or get other cal­en­dar infor­ma­tion using daysInMonth() or week(), as seen in the fol­low­ing example:




    
function output
year(): #year(dateTimeObj)#
month(): #month(dateTimeObj)#
monthAsString(): #monthAsString(month(dateTimeObj))#
day(): #day(dateTimeObj)#
dayOfWeek(): #dayOfWeek(dateTimeObj)#
dayOfWeekAsString(): #dayOfWeekAsString(dayOfWeek(dateTimeObj))#
hour(): #hour(dateTimeObj)#
minute(): #minute(dateTimeObj)#
second(): #second(dateTimeObj)#

dayOfYear(): #dayOfYear(dateTimeObj)#
daysInMonth(): #daysInMonth(dateTimeObj)#
daysInYear(): #daysInYear(dateTimeObj)#
firstDayOfMonth(): #firstDayOfMonth(dateTimeObj)#
quarter(): #quarter(dateTimeObj)#
week(): #week(dateTimeObj)#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

func­tion out­put
year(): 2010
month(): 9
mon­thAs­String(): Sep­tem­ber
day(): 4
day­OfWeek(): 7
day­OfWeekAsString(): Sat­ur­day
hour(): 22
minute(): 43
sec­ond(): 15

day­OfYear(): 247
daysIn­Month(): 30
daysInYear(): 365
first­Day­Of­Month(): 244
quar­ter(): 3
week(): 36

For­mat­ting dates in UTC (Uni­ver­sal Time Code) for­mat in ColdFusion

The fol­low­ing exam­ple shows how you can for­mat dates in UTC for­mat using the getHTTPTimeString() function:



#getHTTPTimeString(dateTimeObj)#

Sun, 05 Sep 2010 05:43:15 GMT

Com­par­ing dates in ColdFusion

The fol­low­ing exam­ple shows how you can com­pare two dates to a spe­cific pre­ci­sion in Cold­Fu­sion using the dateCompare() function:





    
date/time 1: #dateTimeObj1#
date/time 2: #dateTimeObj2#
datePart output
"s" (second) #dateCompare(dateTimeObj1, dateTimeObj2, "s")#
"n" (minute) #dateCompare(dateTimeObj1, dateTimeObj2, "n")#
"h" (hour) #dateCompare(dateTimeObj1, dateTimeObj2, "h")#
"d" (day) #dateCompare(dateTimeObj1, dateTimeObj2, "d")#
"m" (month) #dateCompare(dateTimeObj1, dateTimeObj2, "m")#
"yyyy" (year) #dateCompare(dateTimeObj1, dateTimeObj2, "yyyy")#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

date/​time 1: {ts ‘20100904 22:43:15′}
date/​time 2: {ts ‘20100904 13:43:15′}
datePart out­put
“s” (sec­ond) 1
“n” (minute) 1
“h” (hour) 1
“d” (day) 0
“m” (month) 0
“yyyy” (year) 0

Deter­min­ing the dif­fer­ence between two dates

The fol­low­ing exam­ple shows how you can deter­mine the dif­fer­ence between two dates (in years, months, days, hours, sec­onds, etc) using the dateDiff() function:



 

    
date/time 1: #dateObj1#
date/time 2: #dateObj2#
date part output
"yyyy" (years) #dateDiff("yyyy", dateObj1, dateObj2)#
"q" (quarters) #dateDiff("q", dateObj1, dateObj2)#
"m" (months) #dateDiff("m", dateObj1, dateObj2)#
"y" (days of year) #dateDiff("y", dateObj1, dateObj2)#
"d" (days) #dateDiff("d", dateObj1, dateObj2)#
"w" (weekdays) #dateDiff("w", dateObj1, dateObj2)#
"ww" (weeks) #dateDiff("ww", dateObj1, dateObj2)#
"h" (hours) #dateDiff("h", dateObj1, dateObj2)#
"n" (minutes) #dateDiff("n", dateObj1, dateObj2)#
"s" (seconds) #dateDiff("s", dateObj1, dateObj2)#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

date/​time 1: {ts ‘19700101 00:00:00′}
date/​time 2: {ts ‘20100904 00:00:00′}
date part out­put
“yyyy” (years) 40
“q” (quar­ters) 162
“m” (months) 488
“y” (days of year) 14856
“d” (days) 14856
“w” (week­days) 2122
“ww” (weeks) 2122
“h” (hours) 356543
“n” (min­utes) 21392580
“s” (sec­onds) 1283554800

Con­vert­ing dates between local time and UTC

The fol­low­ing exam­ple shows how you can con­vert dates/​times between local time and UTC time using the dateConvert() function:




 

    
date: #dateObj#
conversion-type output date diff
"utc2local" #dateObjAsLocal# #dateDiff("h", dateObj, dateObjAsLocal)# hours
"local2utc" #dateObjAsUTC# #dateDiff("h", dateObj, dateObjAsUTC)# hours

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

date: {ts ‘20100904 00:00:00′}
conversion-​type out­put date diff
“utc2local” {ts ‘20100903 17:00:00′} 7 hours
“local2utc” {ts ‘20100904 07:00:00′} 7 hours

Get­ting the time zone info of the Cold­Fu­sion Server

The fol­low­ing exam­ple shows how you can get the Cold­Fu­sion Server’s time zone info using the getTimeZoneInfo() function:




    
    
key value
#key# #info[key]#

The pre­vi­ous exam­ple out­puts the fol­low­ing text:

key value
utcHourOff­set 8
utc­To­talOff­set 28800
utcMin­u­te­Off­set 0
isD­STon NO

So you can either access the time zone infor­ma­tion by cre­at­ing a tem­po­rary object, as seen in the fol­low­ing example:



utcHourOffset: #info.utcHourOffset#

Or you can just use the getTimeZoneInfo() method directly, as seen in the fol­low­ing example:

utcHourOffset: #getTimeZoneInfo().utcHourOffset#

The following examples show how you can get started with creating and formatting date values, as well as many other date related functions in ColdFusion.

Getting the current date/time in ColdFusion

To get the current date/time on the ColdFusion server you can use the now() function, as seen in the following example:

<cfoutput>#now()#</cfoutput>

The previous example outputs the following text:

{ts ‘2010-11-12 22:34:52’}

Note: If you want to get the date/time of the user’s computer, you’d need to use a client side language, such as JavaScript.

Creating dates and times in ColdFusion

If you want to create a specific date or date/time, you can use the createDate() and/or createDateTime() functions, as seen in the following example:

<cfset dateObj = createDate(2010, 9, 4) />
<cfset timeObj = createTime(22, 43, 15) />
<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
 
<cfoutput>
<table>
<tr>
    <td>createDate():</td>
    <td>#dateObj#</td>
</tr>
<tr>
    <td>createTime():</td>
    <td>#timeObj#</td>
</tr>
<tr>
    <td>createDateTime():</td>
    <td>#dateTimeObj#</td>
</tr>
</table>
</cfoutput>

The previous example outputs the following text:

createDate(): {ts ‘2010-09-04 00:00:00’}
createTime(): {ts ‘1899-12-30 22:43:15’}
createDateTime(): {ts ‘2010-09-04 22:43:15’}

You can also create ODBC compliant date and/or date time objects in ColdFusion using the createODBCDate() and functions, as seen in the following example:

<cfset dateObj = createDate(2010, 9, 4) />
<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
<cfset odbcDateObj = createODBCDate(dateObj) />
<cfset odbcTimeObj = createODBCTime(dateTimeObj) />
<cfset odbcDateTimeObj = createODBCDateTime(dateTimeObj) />
 
<cfoutput>
<table>
<tr>
    <td>createDate():</td>
    <td>#dateObj#</td>
</tr>
<tr>
    <td>createDateTime():</td>
    <td>#dateTimeObj#</td>
</tr>
<tr>
    <td>createODBCDate():</td>
    <td>#odbcDateObj#</td>
</tr>
<tr>
    <td>createODBCTime():</td>
    <td>#odbcTimeObj#</td>
</tr>
<tr>
    <td>createODBCDateTime():</td>
    <td>#odbcDateTimeObj#</td>
</tr>
</table>
</cfoutput>

The previous example outputs the following text:

createDate(): {ts ‘2010-09-04 00:00:00’}
createDateTime(): {ts ‘2010-09-04 22:43:15’}
createODBCDate(): {d ‘2010-09-04’}
createODBCTime(): {t ’22:43:15′}
createODBCDateTime(): {ts ‘2010-09-04 22:43:15’}

Parsing dates/times in ColdFusion

The following example shows how you can parse a date/time from a string using the parseDateTime() function:

<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
 
<table>
<thead>
    <tr>
        <th>value</th>
        <th>date</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>#dateTimeObj#</td>
        <td>#parseDateTime(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>April 30, 1977</td>
        <td>#parseDateTime("April 30, 1977")#</td>
    </tr>
    <tr>
        <td>Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (pop)</td>
        <td>#parseDateTime("Sat, 4 Sep 2010 21:43:15 -0800 (PDT)", "pop")#</td>
    </tr>
    <tr>
        <td>Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (standard)</td>
        <td>#parseDateTime("Sat, 4 Sep 2010 21:43:15 -0800 (PDT)", "standard")#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

value date
{ts ‘2010-09-04 22:43:15’} {ts ‘2010-09-04 22:43:15’}
April 30, 1977 {ts ‘1977-04-30 00:00:00’}
Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (pop) {ts ‘2010-09-05 05:43:15’}
Sat, 4 Sep 2010 21:43:15 -0800 (PDT) (standard) {ts ‘2010-09-04 21:43:15’}

Note: ColdFusion will generate an error if you pass an invalid date (such as “April 32, 1977”).

Determining if a variable or string is a valid date in ColdFusion

The following example shows how you can determine if a date is valid by using the isDate() function:

<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
 
<table>
<thead>
    <tr>
        <th>value</th>
        <th>is date</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>#dateTimeObj#</td>
        <td>#isDate(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>17</td>
        <td>#isDate(17)#</td>
    </tr>
    <tr>
        <td>April 30, 1977</td>
        <td>#isDate("April 30, 1977")#</td>
    </tr>
    <tr>
        <td>April 31, 1977</td>
        <td>#isDate("April 31, 1977")#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

value is date
{ts ‘2010-09-04 22:43:15’} YES
17 NO
April 30, 1977 YES
April 31, 1977 NO

Adding units of time to a date

To add units of time (years, quarters, months, weeks, hours, etc) to a date, you can use the dateAdd() function, as seen in the following example:

<cfset dateObj = createDate(2000, 1, 1) />
 
<table>
<thead>
    <tr>
        <th>original date</th>
        <th><cfoutput>#dateObj#</cfoutput></th>
    </tr>
    <tr>
        <th>date part</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>yyyy (year)</td>
        <td>#dateAdd("yyyy", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>q (quarter)</td>
        <td>#dateAdd("q", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>m (month)</td>
        <td>#dateAdd("m", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>y (day of year)</td>
        <td>#dateAdd("y", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>d (day)</td>
        <td>#dateAdd("d", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>w (weekday)</td>
        <td>#dateAdd("w", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>ww (week)</td>
        <td>#dateAdd("ww", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>h (hour)</td>
        <td>#dateAdd("h", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>n (minute)</td>
        <td>#dateAdd("n", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>s (second)</td>
        <td>#dateAdd("s", 1, dateObj)#</td>
    </tr>
    <tr>
        <td>l (millisecond)</td>
        <td>#dateAdd("l", 1, dateObj)#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

original date {ts ‘2000-01-01 00:00:00’}
date part output
yyyy (year) {ts ‘2001-01-01 00:00:00’}
q (quarter) {ts ‘2000-04-01 00:00:00’}
m (month) {ts ‘2000-02-01 00:00:00’}
y (day of year) {ts ‘2000-01-02 00:00:00’}
d (day) {ts ‘2000-01-02 00:00:00’}
w (weekday) {ts ‘2000-01-03 00:00:00’}
ww (week) {ts ‘2000-01-08 00:00:00’}
h (hour) {ts ‘2000-01-01 01:00:00’}
n (minute) {ts ‘2000-01-01 00:01:00’}
s (second) {ts ‘2000-01-01 00:00:01’}
l (millisecond) {ts ‘2000-01-01 00:00:00’}

Formatting dates in ColdFusion

To format a date value, you can use the dateFormat() function, as seen in the following example:

<cfoutput>#dateFormat(now())#</cfoutput>

The previous example outputs the following text:

12-Nov-10

As you can see, the default date format is DD-MMM-YY. You can also pass an optional second parameter to the dateFormat() function which represents a “mask” that controls how the date will be shown.

<cfoutput>#dateFormat(now(), "MMM D, YYYY")#</cfoutput>

The previous example outputs the following text:

Nov 12, 2010

There are several different format strings you can use (as well as a few predefined formats such as “short”, “medium”, “long”, and “full”):

<cfset dat = createDate(2010, 9, 4) />
 
<table>
<thead>
    <tr>
        <th>mask</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <!-- d: Day of the month as digits; no leading zero for single-digit days. -->
        <td>"d" (day)</td>
        <td>#dateFormat(dat, "d")#</td>
    </tr>
    <tr>
        <!-- dd: Day of the month as digits; leading zero for single-digit days. -->
        <td>"dd" (day)</td>
        <td>#dateFormat(dat, "dd")#</td>
    </tr>
    <tr>
        <!-- ddd: Day of the week as a three-letter abbreviation. -->
        <td>"ddd" (day abbrev.)</td>
        <td>#dateFormat(dat, "ddd")#</td>
    </tr>
    <tr>
        <!-- dddd: Day of the week as its full name. -->
        <td>"dddd" (day full)</td>
        <td>#dateFormat(dat, "dddd")#</td>
    </tr>
    <tr>
        <!-- m: Month as digits; no leading zero for single-digit months. -->
        <td>"m" (month)</td>
        <td>#dateFormat(dat, "m")#</td>
    </tr>
    <tr>
        <!-- mm: Month as digits; leading zero for single-digit months. -->
        <td>"mm" (month)</td>
        <td>#dateFormat(dat, "mm")#</td>
    </tr>
    <tr>
        <!-- mmm: Month as a three-letter abbreviation. -->
        <td>"mmm" (month abbrev.)</td>
        <td>#dateFormat(dat, "mmm")#</td>
    </tr>
    <tr>
        <!-- mmmm: Month as its full name. -->
        <td>"mmmm" (month full)</td>
        <td>#dateFormat(dat, "mmmm")#</td>
    </tr>
    <tr>
        <!-- yy: Year as last two digits; leading zero for years less than 10. -->
        <td>"yy" (year)</td>
        <td>#dateFormat(dat, "yy")#</td>
    </tr>
    <tr>
        <!-- yyyy: Year represented by four digits. -->
        <td>"yyyy" (year)</td>
        <td>#dateFormat(dat, "yyyy")#</td>
    </tr>
    <tr>
        <!-- gg: Period/era string. -->
        <td>"gg" ("AD")</td>
        <td>#dateFormat(dat, "gg")#</td>
    </tr>
    <tr>
        <!-- short: equivalent to m/d/y -->
        <td>"short" (m/d/y)</td>
        <td>#dateFormat(dat, "short")#</td>
    </tr>
    <tr>
        <!-- medium: equivalent to mmm d, yyyy -->
        <td>"medium" (mmm d, yyyy)</td>
        <td>#dateFormat(dat, "medium")#</td>
    </tr>
    <tr>
        <!-- long: equivalent to mmmm d, yyyy -->
        <td>"long" (mmmm d, yyyy)</td>
        <td>#dateFormat(dat, "long")#</td>
    </tr>
    <tr>
        <!-- full: equivalent to dddd, mmmm d, yyyy -->
        <td>"full" (dddd, mmmm d, yyyy)</td>
        <td>#dateFormat(dat, "full")#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

mask output
“d” (day) 4
“dd” (day) 04
“ddd” (day abbrev.) Sat
“dddd” (day full) Saturday
“m” (month) 9
“mm” (month) 09
“mmm” (month abbrev.) Sep
“mmmm” (month full) September
“yy” (year) 10
“yyyy” (year) 2010
“gg” (“AD”) AD
“short” (m/d/y) 9/4/10
“medium” (mmm d, yyyy) Sep 4, 2010
“long” (mmmm d, yyyy) September 4, 2010
“full” (dddd, mmmm d, yyyy) Saturday, September 4, 2010

Formatting times in ColdFusion

Similarly, you can format times using the timeFormat() function, as seen in the following example:

<cfset dateTimeObj = createDateTime(2010, 9, 4, 21, 43, 15) />
 
<table>
<thead>
    <tr>
        <th>mask</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <!-- h: hours; no leading zero for single-digit hours (12-hour clock) -->
        <td>"h" (hours/12h)</td>
        <td>#timeFormat(dateTimeObj, "h")#</td>
    </tr>
    <tr>
        <!-- hh: hours; leading zero for single-digit hours (12-hour clock) -->
        <td>"hh" (hours/12h)</td>
        <td>#timeFormat(dateTimeObj, "hh")#</td>
    </tr>
    <tr>
        <!-- H: hours; no leading zero for single-digit hours (24-hour clock) -->
        <td>"H" (hours/24h)</td>
        <td>#timeFormat(dateTimeObj, "H")#</td>
    </tr>
    <tr>
        <!-- HH: hours; leading zero for single-digit hours (24-hour clock) -->
        <td>"HH" (hours/24h)</td>
        <td>#timeFormat(dateTimeObj, "HH")#</td>
    </tr>
    <tr>
        <!-- m: minutes; no leading zero for single-digit minutes -->
        <td>"m" (minutes)</td>
        <td>#timeFormat(dateTimeObj, "m")#</td>
    </tr>
    <tr>
        <!-- mm: minutes; a leading zero for single-digit minutes -->
        <td>"mm" (minutes)</td>
        <td>#timeFormat(dateTimeObj, "mm")#</td>
    </tr>
    <tr>
        <!-- s: seconds; no leading zero for single-digit seconds -->
        <td>"s" (minutes)</td>
        <td>#timeFormat(dateTimeObj, "s")#</td>
    </tr>
    <tr>
        <!-- ss: seconds; leading zero for single-digit seconds -->
        <td>"ss" (minutes)</td>
        <td>#timeFormat(dateTimeObj, "ss")#</td>
    </tr>
    <tr>
        <!-- l or L: milliseconds, with no leading zeros -->
        <td>"l" (milliseconds)</td>
        <td>#timeFormat(dateTimeObj, "l")#</td>
    </tr>
    <tr>
        <!-- t: one-character time marker string, such as A or P -->
        <td>"t" (A/P)</td>
        <td>#timeFormat(dateTimeObj, "t")#</td>
    </tr>
    <tr>
        <!-- tt: multiple-character time marker string, such as AM or PM -->
        <td>"tt" (AM/PM)</td>
        <td>#timeFormat(dateTimeObj, "tt")#</td>
    </tr>
    <tr>
        <!-- short: equivalent to h:mm tt -->
        <td>"short" (h:mm tt)</td>
        <td>#timeFormat(dateTimeObj, "short")#</td>
    </tr>
    <tr>
        <!-- medium: equivalent to h:mm:ss tt -->
        <td>"medium" (h:mm:ss tt)</td>
        <td>#timeFormat(dateTimeObj, "medium")#</td>
    </tr>
    <tr>
        <!-- long: medium followed by three-letter time zone; as in, 2:34:55 PM EST -->
        <td>"long"</td>
        <td>#timeFormat(dateTimeObj, "long")#</td>
    </tr>
    <tr>
        <!-- full: same as long -->
        <td>"full"</td>
        <td>#timeFormat(dateTimeObj, "full")#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

mask output
“h” (hours/12h) 9
“hh” (hours/12h) 09
“H” (hours/24h) 21
“HH” (hours/24h) 21
“m” (minutes) 43
“mm” (minutes) 43
“s” (minutes) 15
“ss” (minutes) 15
“l” (milliseconds) 0
“t” (A/P) P
“tt” (AM/PM) PM
“short” (h:mm tt) 9:43 PM
“medium” (h:mm:ss tt) 9:43:15 PM
“long” 9:43:15 PM PDT
“full” 9:43:15 PM PDT

Getting individual date parts in ColdFusion

You can also get individual date/time parts using specific functions such as year(), month(), hour(), second(), or get other calendar information using daysInMonth() or week(), as seen in the following example:

<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
 
<table>
<thead>
    <tr>
        <th>function</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>year():</td>
        <td>#year(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>month():</td>
        <td>#month(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>monthAsString():</td>
        <td>#monthAsString(month(dateTimeObj))#</td>
    </tr>
    <tr>
        <td>day():</td>
        <td>#day(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>dayOfWeek():</td>
        <td>#dayOfWeek(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>dayOfWeekAsString():</td>
        <td>#dayOfWeekAsString(dayOfWeek(dateTimeObj))#</td>
    </tr>
    <tr>
        <td>hour():</td>
        <td>#hour(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>minute():</td>
        <td>#minute(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>second():</td>
        <td>#second(dateTimeObj)#</td>
    </tr>
    <tr>
        <td colspan="2"><hr/></td>
    </tr>
    <tr>
        <td>dayOfYear():</td>
        <td>#dayOfYear(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>daysInMonth():</td>
        <td>#daysInMonth(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>daysInYear():</td>
        <td>#daysInYear(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>firstDayOfMonth():</td>
        <td>#firstDayOfMonth(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>quarter():</td>
        <td>#quarter(dateTimeObj)#</td>
    </tr>
    <tr>
        <td>week():</td>
        <td>#week(dateTimeObj)#</td>
    </tr>
    </cfoutput>
</tbody>
</table>
</cfoutout>

The previous example outputs the following text:

function output
year(): 2010
month(): 9
monthAsString(): September
day(): 4
dayOfWeek(): 7
dayOfWeekAsString(): Saturday
hour(): 22
minute(): 43
second(): 15

dayOfYear(): 247
daysInMonth(): 30
daysInYear(): 365
firstDayOfMonth(): 244
quarter(): 3
week(): 36

Formatting dates in UTC (Universal Time Code) format in ColdFusion

The following example shows how you can format dates in UTC format using the getHTTPTimeString() function:

<cfset dateTimeObj = createDateTime(2010, 9, 4, 22, 43, 15) />
 
<cfoutput>#getHTTPTimeString(dateTimeObj)#</cfoutput>

Sun, 05 Sep 2010 05:43:15 GMT

Comparing dates in ColdFusion

The following example shows how you can compare two dates to a specific precision in ColdFusion using the dateCompare() function:

<cfset dateTimeObj1 = createDateTime(2010, 9, 4, 22, 43, 15) />
<cfset dateTimeObj2 = createDateTime(2010, 9, 4, 13, 43, 15) />
 
<table>
<thead>
    <tr>
        <td>date/time 1:</td>
        <td><cfoutput>#dateTimeObj1#</cfoutput></td>
    </tr>
    <tr>
        <td>date/time 2:</td>
        <td><cfoutput>#dateTimeObj2#</cfoutput></td>
    </tr>
    <tr>
        <th>datePart</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>"s" (second)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "s")#</td>
    </tr>
    <tr>
        <td>"n" (minute)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "n")#</td>
    </tr>
    <tr>
        <td>"h" (hour)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "h")#</td>
    </tr>
    <tr>
        <td>"d" (day)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "d")#</td>
    </tr>
    <tr>
        <td>"m" (month)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "m")#</td>
    </tr>
    <tr>
        <td>"yyyy" (year)</td>
        <td>#dateCompare(dateTimeObj1, dateTimeObj2, "yyyy")#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

date/time 1: {ts ‘2010-09-04 22:43:15’}
date/time 2: {ts ‘2010-09-04 13:43:15’}
datePart output
“s” (second) 1
“n” (minute) 1
“h” (hour) 1
“d” (day) 0
“m” (month) 0
“yyyy” (year) 0

Determining the difference between two dates

The following example shows how you can determine the difference between two dates (in years, months, days, hours, seconds, etc) using the dateDiff() function:

<cfset dateObj1 = createDate(1970, 1, 1) />
<cfset dateObj2 = createDate(2010, 9, 4) />
 
<table>
<thead>
    <tr>
        <td>date/time 1:</td>
        <td><cfoutput>#dateObj1#</cfoutput></td>
    </tr>
    <tr>
        <td>date/time 2:</td>
        <td><cfoutput>#dateObj2#</cfoutput></td>
    </tr>
    <tr>
        <th>date part</th>
        <th>output</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>"yyyy" (years)</td>
        <td>#dateDiff("yyyy", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"q" (quarters)</td>
        <td>#dateDiff("q", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"m" (months)</td>
        <td>#dateDiff("m", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"y" (days of year)</td>
        <td>#dateDiff("y", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"d" (days)</td>
        <td>#dateDiff("d", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"w" (weekdays)</td>
        <td>#dateDiff("w", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"ww" (weeks)</td>
        <td>#dateDiff("ww", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"h" (hours)</td>
        <td>#dateDiff("h", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"n" (minutes)</td>
        <td>#dateDiff("n", dateObj1, dateObj2)#</td>
    </tr>
    <tr>
        <td>"s" (seconds)</td>
        <td>#dateDiff("s", dateObj1, dateObj2)#</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

date/time 1: {ts ‘1970-01-01 00:00:00’}
date/time 2: {ts ‘2010-09-04 00:00:00’}
date part output
“yyyy” (years) 40
“q” (quarters) 162
“m” (months) 488
“y” (days of year) 14856
“d” (days) 14856
“w” (weekdays) 2122
“ww” (weeks) 2122
“h” (hours) 356543
“n” (minutes) 21392580
“s” (seconds) 1283554800

Converting dates between local time and UTC

The following example shows how you can convert dates/times between local time and UTC time using the dateConvert() function:

<cfset dateObj = createDate(2010, 9, 4) />
<cfset dateObjAsLocal = dateConvert("utc2local", dateObj) />
<cfset dateObjAsUTC = dateConvert("local2utc", dateObj) />
 
<table>
<thead>
    <tr>
        <td>date:</td>
        <td><cfoutput><em>#dateObj#</em></cfoutput></td>
    </tr>
    <tr>
        <th>conversion-type</th>
        <th>output</th>
        <th>date diff</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <tr>
        <td>"utc2local"</td>
        <td>#dateObjAsLocal#</td>
        <td>#dateDiff("h", dateObj, dateObjAsLocal)# hours</td>
    </tr>
    <tr>
        <td>"local2utc"</td>
        <td>#dateObjAsUTC#</td>
        <td>#dateDiff("h", dateObj, dateObjAsUTC)# hours</td>
    </tr>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

date: {ts ‘2010-09-04 00:00:00’}
conversion-type output date diff
“utc2local” {ts ‘2010-09-03 17:00:00’} -7 hours
“local2utc” {ts ‘2010-09-04 07:00:00’} 7 hours

Getting the time zone info of the ColdFusion Server

The following example shows how you can get the ColdFusion Server’s time zone info using the getTimeZoneInfo() function:

<cfset info = getTimeZoneInfo() />
 
<table>
<thead>
    <tr>
        <th>key</th>
        <th>value</th>
    </tr>
</thead>
<tbody>
    <cfoutput>
    <cfloop collection="#info#" item="key">
    <tr>
        <td>#key#</td>
        <td>#info[key]#</td>
    </tr>
    </cfloop>
    </cfoutput>
</tbody>
</table>

The previous example outputs the following text:

key value
utcHourOffset 8
utcTotalOffset 28800
utcMinuteOffset 0
isDSTon NO

So you can either access the time zone information by creating a temporary object, as seen in the following example:

<cfset info = getTimeZoneInfo() />
 
<cfoutput>utcHourOffset: #info.utcHourOffset#</cfoutput>

Or you can just use the getTimeZoneInfo() method directly, as seen in the following example:

<cfoutput>utcHourOffset: #getTimeZoneInfo().utcHourOffset#</cfoutput>

Leave a Reply

Your email address will not be published.