Built-in Functions

Contact Us or call 1-877-932-8228
Built-in Functions

Built-in Functions

ColdFusion has many built-in functions for performing all sorts of tasks, such as manipulating strings, formatting data, managing dates and times, parsing XML, etc.

The syntax of a function is functionname(arguments), where arguments is a list of zero or more values passed to the function.

Like variables, function names are case-insensitive.

It is impossible (at least for this author) to memorize all the function names and the parameters that each function takes. So, every ColdFusion developer should have a good function reference. Following are few suggestions:

ColdFusion Function References

We will be looking at many functions throughout this course, but to give you a feel for what they how they work, here are a few date and time functions.

Date & Time Functions

Function Description
Now() Returns the current date and time on the server.
DateFormat("date" [,"mask"]) Formats the date as specified by the mask.
TimeFormat("time" [,"mask"]) Formats the time as specified by the mask.
CreateDate(year, month, day) Creates a date/time object with the time set to 00:00:00.
IsDate(string) Returns true if a string can be converted to a valid date/time object.
DaysInMonth("date") Returns the number of days in a month.

The file below shows these functions in practice:

Code Sample:

Basics/Demos/DateFunctions.cfm
<html>
<head>
<title>Date Functions</title>
</head>
<body>
<cfset RightNow = Now()>
<cfoutput>
	#RightNow#<br />
	#DateFormat(RightNow)#<br />
	#DateFormat(RightNow,"mm/dd/yy")#<br />
	#TimeFormat(RightNow)#<br />
	#TimeFormat(RightNow,"hh:mm tt")#<br />
	#IsDate(RightNow)#<br />
	#IsDate("January 31, 2007")#<br />
	#IsDate("foo")#<br />
	#DaysInMonth(RightNow)#
</cfoutput>
</body>
</html>

The output is shown below:

And here is another demo showing a friendlier page using Now() and DateFormat():

Code Sample:

Basics/Demos/HelloWorldDate.cfm
<cfset greeting="Hello">
<cfset today = Now()>

<html>
<head>
	<title>Hello World!</title>
</head>
<body>
	<cfoutput>
		#greeting#, World!<br>
		Today is #DateFormat(Now(),"dddd, mmmm d, yyyy")#.
	</cfoutput>
</body>
</html>

The output is shown below:

Pound Sign Usage

You may notice that no pound signs are used around the Now() function in the <cfset> tag, while there are pound signs around the DateFormat() function. The use of pound signs to indicate that a variable or expression should be evaluated can be a bit confusing. Here are the rules:

  • Use pound signs when the expression to be evaluated is not within a ColdFusion tag (e.g, <cfoutput>#Now()#</cfoutput>.
  • Use pound signs around expressions that are within a ColdFusion tag ONLY when the expression is also in quotes (e.g, <cfset greeting = "Hello #person#">).
  • With nested functions, use pound signs only around the outermost function (e.g, <cfoutput>#DateFormat(Now())#</cfoutput>).

Examples of Proper and Improper usage of pound signs

Proper Usage

<cfset person = "Paul"> <cfset greeting = "Hello #person#"> <cfoutput> #greeting#, today is #DateFormat(Now(),"dddd")#. </cfoutput>

Improper Usage

<!---The improper use is italicized---> <cfset person = "Paul"> <cfset greeting = "Hello" & #person#>

Improper Usage

<!---The improper use is italicized---> <cfoutput> #greeting#, today is #DateFormat(#Now()#,"dddd")#. </cfoutput>
Next