DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 2 · LESSON 08

Date & Time

Working with dates and times in ColdFusion — now(), createDate(), dateFormat(), dateAdd(), dateDiff(), dateCompare(), and isDate(), in both CFScript and Tag Syntax.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

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.

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

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
<cfscript>

birthday = createDate(1998,8,15);

writeOutput(birthday);

</cfscript>
Tag Syntax
<cfset birthday = createDate(1998,8,15)>

<cfoutput>
#birthday#
</cfoutput>

Creating Date & Time

Use createDateTime().

CFScript
<cfscript>

meeting = createDateTime(
    2026,
    12,
    25,
    10,
    30,
    0
);

writeOutput(meeting);

</cfscript>
Tag Syntax
<cfset meeting = createDateTime(
    2026,
    12,
    25,
    10,
    30,
    0
)>

<cfoutput>
#meeting#
</cfoutput>

Formatting Dates

Dates can be displayed in many formats. Use dateFormat().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Formatting Time

Use timeFormat().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Formatting Date & Time Together

Use dateTimeFormat().

CFScript
<cfscript>

writeOutput(
    dateTimeFormat(
        now(),
        "dd-mmm-yyyy hh:mm tt"
    )
);

</cfscript>
Tag Syntax
<cfoutput>
#dateTimeFormat(
    now(),
    "dd-mmm-yyyy hh:mm tt"
)#
</cfoutput>

Adding Time

Use dateAdd().

CFScript
<cfscript>

nextWeek = dateAdd(
    "d",
    7,
    now()
);

writeOutput(nextWeek);

</cfscript>
Tag Syntax
<cfset nextWeek =
dateAdd(
    "d",
    7,
    now()
)>

<cfoutput>
#nextWeek#
</cfoutput>

Finding the Difference

Use dateDiff().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Comparing Dates

Use dateCompare().

ValueMeaning
-1First date is earlier
0Dates are equal
1First date is later
CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Extracting Date Parts

ColdFusion provides many helper functions.

FunctionExample
year()2026
month()8
day()2
hour()10
minute()45
second()20
quarter()3
week()31

Extracting a Date Part — Example

CFScript
<cfscript>

today = now();

writeOutput(
year(today)
);

</cfscript>
Tag Syntax
<cfset today = now()>

<cfoutput>
#year(today)#
</cfoutput>

Checking Whether a Value Is a Date

Use isDate().

CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Real-World Example

CFScript
<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>
Tag Syntax
<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

FunctionPurpose
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.