Almost every web application works with dates and times. You might need to show today's date, record when a user logs in, calculate a future or past date, find the difference between two dates, format a date for display, or extract the day, month, or year.
ColdFusion has excellent built-in support for working with dates and times. Unlike some languages that require external libraries, CFML includes dozens of functions for creating, formatting, comparing, and manipulating dates.
One important thing to remember is that ColdFusion does not have a built-in today() function. Instead, use now() to get the current date and time.
By the end of this lesson, you'll know how to create dates, format them, perform date calculations, compare dates, and use the most common date and time functions in both CFScript and Tag Syntax.
Learning Objectives
After completing this lesson, you'll be able to:
- Understand ColdFusion Date & Time objects.
- Get the current date and time.
- Create custom dates and times.
- Format dates and times.
- Add or subtract dates.
- Compare dates.
- Calculate the difference between dates.
- Extract individual parts of a date.
- Validate date values.
What Is a Date Object?
A date object stores both the date and the time. Example: 02-Aug-2026 10:45:20 AM.
Unlike a plain string, a date object can be compared, formatted, and manipulated using ColdFusion's built-in functions.
Getting the Current Date & Time
ColdFusion uses the now() function.
ColdFusion does not provide a built-in today() function. Use now() whenever you need the current date and time.
Creating a Date
Use createDate().
<cfscript> birthday = createDate(1998,8,15); writeOutput(birthday); </cfscript>
<cfset birthday = createDate(1998,8,15)> <cfoutput> #birthday# </cfoutput>
Creating Date & Time
Use createDateTime().
<cfscript>
meeting = createDateTime(
2026,
12,
25,
10,
30,
0
);
writeOutput(meeting);
</cfscript><cfset meeting = createDateTime(
2026,
12,
25,
10,
30,
0
)>
<cfoutput>
#meeting#
</cfoutput>Formatting Dates
Dates can be displayed in many formats. Use dateFormat().
Formatting Time
Use timeFormat().
Formatting Date & Time Together
Use dateTimeFormat().
<cfscript>
writeOutput(
dateTimeFormat(
now(),
"dd-mmm-yyyy hh:mm tt"
)
);
</cfscript><cfoutput>
#dateTimeFormat(
now(),
"dd-mmm-yyyy hh:mm tt"
)#
</cfoutput>Adding Time
Use dateAdd().
<cfscript>
nextWeek = dateAdd(
"d",
7,
now()
);
writeOutput(nextWeek);
</cfscript><cfset nextWeek =
dateAdd(
"d",
7,
now()
)>
<cfoutput>
#nextWeek#
</cfoutput>Finding the Difference
Use dateDiff().
Comparing Dates
Use dateCompare().
| Value | Meaning |
|---|---|
| -1 | First date is earlier |
| 0 | Dates are equal |
| 1 | First date is later |
Extracting Date Parts
ColdFusion provides many helper functions.
| Function | Example |
|---|---|
| year() | 2026 |
| month() | 8 |
| day() | 2 |
| hour() | 10 |
| minute() | 45 |
| second() | 20 |
| quarter() | 3 |
| week() | 31 |
Extracting a Date Part — Example
<cfscript> today = now(); writeOutput( year(today) ); </cfscript>
<cfset today = now()> <cfoutput> #year(today)# </cfoutput>
Checking Whether a Value Is a Date
Use isDate().
Real-World Example
<cfscript>
orderDate = now();
deliveryDate =
dateAdd(
"d",
5,
orderDate
);
writeOutput(
"Order Date: " &
dateFormat(orderDate,"dd-mmm-yyyy")
);
writeOutput("<br>");
writeOutput(
"Delivery Date: " &
dateFormat(deliveryDate,"dd-mmm-yyyy")
);
</cfscript><cfset orderDate = now()>
<cfset deliveryDate =
dateAdd("d",5,orderDate)>
<cfoutput>
Order Date:
#dateFormat(orderDate,"dd-mmm-yyyy")#
<br>
Delivery Date:
#dateFormat(deliveryDate,"dd-mmm-yyyy")#
</cfoutput>Common Beginner Mistakes
Using today()
ColdFusion has no today() function and calling it throws an error. Use now() instead to get the current date and time.
Comparing Date Strings
Comparing formatted strings like "02-Aug-2026" can give unreliable results. Always compare date objects, not formatted strings.
Storing Dates as Strings
Prefer using createDate() or createDateTime() instead of manually typing date strings whenever possible. ColdFusion stores and manipulates true date objects more reliably than plain text.
Best Practices
- Use now() for the current date and time.
- Use createDate() and createDateTime() to build dates.
- Use dateFormat() only for display.
- Store dates as date objects, not strings.
- Use dateAdd() instead of manual calculations.
- Validate user input with isDate().
Frequently Used Date Functions
| Function | Purpose |
|---|---|
| now() | Current date & time |
| createDate() | Create a date |
| createDateTime() | Create a date & time |
| dateAdd() | Add days, months, years |
| dateDiff() | Difference between dates |
| dateCompare() | Compare two dates |
| dateFormat() | Format a date |
| timeFormat() | Format a time |
| dateTimeFormat() | Format date & time |
| year() | Get year |
| month() | Get month |
| day() | Get day |
| hour() | Get hour |
| minute() | Get minute |
| second() | Get second |
| week() | Week number |
| quarter() | Quarter of year |
| isDate() | Validate a date |
Interview Questions
Does ColdFusion have a today() function?
No. Use now() to get the current date and time.
Which function formats only the date?
dateFormat()
Which function formats only the time?
timeFormat()
Which function adds days or months to a date?
dateAdd()
Which function compares two dates?
dateCompare()
Which function returns the difference between two dates?
dateDiff()
Summary
In this lesson, you learned how ColdFusion works with Date & Time values. You used now() to get the current date and time, created custom dates with createDate() and createDateTime(), formatted dates using dateFormat(), timeFormat(), and dateTimeFormat(), performed calculations with dateAdd() and dateDiff(), compared dates using dateCompare(), extracted date parts such as year and month, and validated dates with isDate(). These functions cover the vast majority of date and time operations you'll use in everyday ColdFusion development.
What's Next?
The next lesson covers Type Conversion — how to convert values between strings, numbers, booleans, dates, and other data types using functions like val(), int(), parseNumber(), parseDateTime(), and related conversion techniques.