ColdFusion Programming/dates

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Coldfusion can handle the creation, modification, display, and usage of dates.

Setting a Date[edit | edit source]

In order to set a date use the cfset function.

<cfset mydate = '12/3/2006'>

Displaying a Date[edit | edit source]

The basic display of a date is simply to output it.

<cfoutput>#mydate#</cfoutput>

This would display: 12/3/2006

There is function that allows you to easily convert a date into another format.

<cfoutput>#dateformat(mydate, 'yyyy/mm/dd')#</cfoutput>

This would display: 2006/12/3

The options to be used here are:

  • d: Day of the month as digits, no leading zeros
  • dd: Day of the month as digits
  • ddd: Day of the week, abbr.
  • dddd: Day of the week, name
  • m: Month as digits, no leading zero
  • mm: Month as digits
  • mmm: Month as abbr.
  • mmmm: Month as name.
  • y: Year - last two digits, no leading zeros
  • yy: Year - last two digits
  • yyyy: Year - four digits

The options can be separated by - / , or space to create the format that you want.

Modifying a date[edit | edit source]

If we want to add 5 days to today we would use this function.

<cfset todayplus5 = dateadd('d', 5, now())> 

The options for dateadd are:

  • yyyy: Year
  • q: Quarter
  • m: Month
  • y: Day of year
  • d: Day
  • w: Weekday
  • ww: Week
  • h: Hour
  • n: Minute (remember m is for month not minute)
  • s: Second