Whatever message this page gives is out now! Go check it out!

DateFormat

Last update:
May 18, 2026

Description

Formats a date value using U.S. date formats. For international date support, use LSDateFormat.

Returns

A text string representing the date formatted according to the mask. If no mask is specified, returns the value in dd-mmm-yy format.

Category

Function syntax

DateFormat(date [, mask])

See also

History

ColdFusion (2021 release) Update 1: We've fixed the issue with mask in the function. For more information, see the bug. We recommend updating to Update 1.
ColdFusion (2021 release): Added a JVM flag -Dcoldfusion.datemask.useDasdayofmonth. It defaults to false but when set to true and the mask contains D (uppercase D), the mask treats the value as d (lowercase d), day of the month. Hence, dateformat(now(), "mm-D-yyyy") is the same as dateformat(now(), "mm-d-yyyy") when flag is set to true. By default Capital D is used to specify Day of the year. See example below.
ColdFusion (2018 release) Update 3. ColdFusion (2016 release) Update 10, and ColdFusion 11 Update 18: Input formats "m/d" or "d/m", where masks m as month and d as date with valid month/day numerals are no more considered as valid date input. For example, see Example 2.
ColdFusion (2016 release) Update 3: Contains the following changes:
  • You can use both lowercase and uppercase letters as mask characters.
  • The following masks are added:
    • e/E: Day in a week.
    • f/F: Day of a week in a month.
    • k/K: Hour in a day (1-24).
    • w: Week of the year as  digit .
    • ww : Week of the year as digits. Leading zero for  single-digit  week.
ColdFusion MX: Added support for the following mask parameter options: short, medium, long, and full.

Parameters

Parameter
Description
date
Date/time object, in the range 100 AD-9999 AD.
mask
Characters that show how ColdFusion displays a date:
  • d: Day of the month as digits; no leading zero for single-digit days.
  • dd: Day of the month as digits; leading zero for single-digit days.
  • ddd : Day of the week as a three-letter abbreviation.
  • dddd : Day of the week as its full name.
  • eee /EEE: Day of the week as a three-letter abbreviation.
  • eeee /EEEE: Day of the week as its full name.
  • f/F: Day of a week in a month.
  • m: Month as digits; no leading zero for single-digit months.
  • mm: Month as digits; leading zero for single-digit months.
  • mmm: Month as a three-letter abbreviation.
  • mmmm: Month as its full name.
  • M: Month in year .
  • D: Day in year .
  • k/K: Hour in a day.
  • w: Week of the year as  digit .
  • ww : Week of the year as digits. Leading zero for  single-digit  week.
  • W: Week of the month as  digit .
  • WW: Week of the month as digits. Leading zero for  single-digit  week.
  • Y: Week year.
  • YY: Week year as last two digits; leading zero for years less than 10.
  • yy : Year as last two digits; leading zero for years less than 10.
  • yyyy /YYYY: Year represented by four digits.
  • gg: Period/era string. Ignored. Reserved. The following masks tell how to format the full date and cannot be combined with other masks:
  • short: equivalent to m/d/y
  • medium: equivalent to mmm d, yyyy
  • long: equivalent to mmmm d, yyyy
  • full: equivalent to dddd , mmmm d, yyyy
  • z: Time zone in literal format, for example, IST
  • Z: Time zone in hours of offset (RFC 822 TimeZone), for example, +0530
  • X: Time zone in hours of offset in ISO 8601 format. The following are the three ways of using 'X':
    • X: +05
    • XX: +0530
    • XXX: +5:30

Usage

When passing a date/time object as a string, enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.
Date and time values in database query results can vary in sequence and formatting unless you use functions to format them. To ensure that application users correctly understand displayed dates and times, Adobe recommends that you use this function and the LSDateFormat and LSTimeFormat functions to format resultset values.
The DateFormat function is best used for formatting output, not for formatting input. For formatting input, use one of the date/time creation functions (for example, CreateDate) instead.

Example  1

<cfscript>
    Date1 = "{ts '2018-11-15 12:13:50'}";
    dateformat= DateFormat(Date1)
    dateformat1= DateFormat(Date1,"e")
    dateformat2= DateFormat(Date1,"f")
    dateformat3= DateFormat(Date1,"k")
    dateformat4= DateFormat(Date1,"w")
    dateformat5= DateFormat(Date1,"ww")
    writeOutput("date is : " & dateformat & "<br/>")
    writeOutput("day in a week is : " & dateformat1 & "<br/>")
    writeOutput("day of a week in a month : " & dateformat2 & "<br/>")
    writeOutput("hour in a day: " & dateformat3 & "<br/>")
    writeOutput("week of the year as a digit: " & dateformat4 & "<br/>")
    writeOutput("Week of the year as digits. Leading zero for single-digit week. : " & dateformat5 & "<br/>")
</cfscript>
Output
date is : 15-Nov-18

day in a week is : Thu

day of a week in a month : 3

hour in a day: 12

week of the year as a digit: 46

Week of the year as digits. Leading zero for single-digit week. : 46
 
Example 2
<cfscript> 
 writeOutput("The date is: " & DateFormat('04/10/2019','mm-dd')) 
</cfscript>
The code above runs as expected and produces the following output:
The date is: 04-10
However, the code below throws an exception since the input date is in an incorrect format.
<cfscript> 
 // This snippet throws an exception 
 writeOutput("The date is: " & DateFormat('04/10','mm-dd')) 
</cfscript>
coldfusion.datemask.useDasdayofmonth
If you set the above flag as D, the snippet below produces the same output when the mask is set to d.
<cfscript> 
    writeOutput(dateformat(now(), "mm-D-yyyy") & "<br/>") 
    writeOutput(dateformat(now(), "mm-d-yyyy") & "<br/>") 
</cfscript>

Additional real-world examples for DateFormat function

The following examples demonstrate additional real-world uses of the DateFormat function.

User-friendly date display in web applications

Transform technical datetime values into human-readable formats throughout your web application to improve user experience and professionalism.  This is important because:
  • Users prefer "October 5, 2025" over raw timestamps like "2025-10-05 14:30:22"
  • Increases readability and reduces cognitive load
  • Improves perceived application quality
In modern web applications, dates appear everywhere - from dashboard headers to content timestamps. Consider a SaaS platform where users log in daily: showing "October 5, 2025" instead of a database timestamp creates a more welcoming, professional experience.
<cfscript>
    currentDate = now();
    formattedDate = dateFormat(currentDate, "mmmm d, yyyy");
</cfscript>

<!DOCTYPE html>
<html>
<head>
    <title>Welcome Page</title>
</head>
<body>
    <div class="page-header">
        <h1>Welcome to Our Application</h1>
        <cfoutput>
            <p class="text-muted">Today's date: #formattedDate#</p>
        </cfoutput>
    </div>
</body>
</html>
Using a component
components/DateHelper.cfc
component displayname="DateHelper" hint="Utility component for date formatting" {
    
    public string function getFormattedDate(
        required date dateValue,
        string format = "mmmm d, yyyy"
    ) hint="Formats a date value with error handling" {
        
        try {
            if (isDate(arguments.dateValue)) {
                return dateFormat(arguments.dateValue, arguments.format);
            } else {
                return "Invalid Date";
            }
        } catch (any e) {
            writeLog(file="datehelper", text="Error formatting date: #e.message#");
            return "Date Error";
        }
    }
    
    public string function getRelativeDate(required date dateValue) {
        var daysDiff = dateDiff("d", arguments.dateValue, now());
        
        if (daysDiff == 0) {
            return "Today";
        } else if (daysDiff == 1) {
            return "Yesterday";
        } else if (daysDiff < 7) {
            return "#daysDiff# days ago";
        } else {
            return dateFormat(arguments.dateValue, "mmmm d, yyyy");
        }
    }
    
}
ViewPage.cfm
<cfscript>
    dateHelper = createObject("component", "components.DateHelper");
    publishDate = createDate(2025, 10, 3);
</cfscript>

<!DOCTYPE html>
<html>
<body>
    <cfoutput>
        <h2>Date Formatting Examples</h2>
        
        <p>Long format: #dateHelper.getFormattedDate(now(), "mmmm d, yyyy")#</p>
        <p>Short format: #dateHelper.getFormattedDate(now(), "mm/dd/yyyy")#</p>
        <p>Abbreviated: #dateHelper.getFormattedDate(now(), "mmm d, yy")#</p>
        
        <h3>Article Published</h3>
        <p>Published: #dateHelper.getRelativeDate(publishDate)#</p>
    </cfoutput>
</body>
</html>

eCommerce order confirmation pages

Display order dates consistently across confirmations, invoices, and customer accounts. This is important because:
  • Customers need clear timestamps for tracking and returns
  • Consistent formatting reduces confusion
  • Legal requirements for order documentation
eCommerce businesses process thousands of orders daily, and customers frequently reference order dates for tracking shipments, initiating returns (often with 30-day windows), and managing warranties.
<cfscript>
    param name="url.orderID" type="numeric";
    
    qOrder = queryExecute("
        SELECT 
            o.orderID,
            o.customerName,
            o.customerEmail,
            o.orderDate,
            o.totalAmount,
            o.status,
            o.shippingMethod
        FROM orders o
        WHERE o.orderID = :orderID
    ", {
        orderID: {value: url.orderID, cfsqltype: "cf_sql_integer"}
    }, {
        datasource: "ecommerce"
    });
</cfscript>

<!DOCTYPE html>
<html>
<head>
    <title>Order Confirmation</title>
    <style>
        .order-confirmation { max-width: 800px; margin: 0 auto; padding: 20px; }
        .order-header { background: #f8f9fa; padding: 20px; margin-bottom: 20px; }
        .order-details { border: 1px solid #dee2e6; padding: 15px; }
        .detail-row { display: flex; justify-content: space-between; padding: 10px 0; }
        .label { font-weight: bold; }
    </style>
</head>
<body>
    <cfoutput query="qOrder">
    <div class="order-confirmation">
        <div class="order-header">
            <h1>✓ Order Confirmed!</h1>
            <h2>Order ##############orderID#</h2>
        </div>
        
        <div class="order-details">
            <div class="detail-row">
                <span class="label">Order Date:</span>
                <span>#dateFormat(orderDate, "mmmm d, yyyy")#</span>
            </div>
            
            <div class="detail-row">
                <span class="label">Order Time:</span>
                <span>#timeFormat(orderDate, "h:mm tt")#</span>
            </div>
            
            <div class="detail-row">
                <span class="label">Customer:</span>
                <span>#customerName#</span>
            </div>
            
            <div class="detail-row">
                <span class="label">Total Amount:</span>
                <span>#dollarFormat(totalAmount)#</span>
            </div>
            
            <div class="detail-row">
                <span class="label">Status:</span>
                <span>#status#</span>
            </div>
        </div>
        
        <cfscript>
            estimatedDelivery = dateAdd("d", 5, orderDate);
        </cfscript>
        
        <div class="estimated-delivery">
            <h3>Estimated Delivery</h3>
            <p>#dateFormat(estimatedDelivery, "dddd, mmmm d, yyyy")#</p>
        </div>
    </div>
    </cfoutput>
</body>
</html>

Blog post publishing dates

Display article publication dates in blogs and content management systems with various format options. This is important because:
  • Content freshness matters for SEO
  • Users assess content relevance by date
  • RSS feeds require specific date formats
Content creators understand that publication dates significantly impact credibility and SEO rankings. Google's algorithm considers content freshness, especially for news and time-sensitive topics.
<cfscript>
    param name="url.page" type="numeric" default=1;
    postsPerPage = 10;
    startRow = (url.page - 1) * postsPerPage + 1;
    
    qBlogPosts = queryExecute("
        SELECT 
            postID,
            title,
            author,
            publishedDate,
            excerpt,
            categoryName,
            viewCount
        FROM blog_posts
        WHERE status = 'published'
        ORDER BY publishedDate DESC
        LIMIT :startRow, :postsPerPage
    ", {
        startRow: {value: startRow, cfsqltype: "cf_sql_integer"},
        postsPerPage: {value: postsPerPage, cfsqltype: "cf_sql_integer"}
    }, {
        datasource: "cms"
    });
</cfscript>

<!DOCTYPE html>
<html>
<head>
    <title>Blog Posts</title>
    <style>
        .blog-listing { max-width: 900px; margin: 0 auto; padding: 20px; }
        .blog-post-preview { border-bottom: 1px solid #eee; padding: 20px 0; }
        .post-meta { color: #666; font-size: 14px; margin: 10px 0; }
        .post-meta span { margin-right: 15px; }
        .badge-new { background: #ff4444; color: white; padding: 3px 8px; border-radius: 3px; }
        .excerpt { color: #444; line-height: 1.6; }
    </style>
</head>
<body>
    <div class="blog-listing">
        <h1>Latest Blog Posts</h1>
        
        <cfoutput query="qBlogPosts">
            <article class="blog-post-preview">
                <h2><a href="post.cfm?id=#postID#">#title#</a></h2>
                
                <div class="post-meta">
                    <span class="author">By #author#</span>
                    <span class="date">
                        #dateFormat(publishedDate, "mmmm d, yyyy")#
                    </span>
                    <span class="category">#categoryName#</span>
                    <span class="views">#viewCount# views</span>
                </div>
                
                <p class="excerpt">#excerpt#</p>
                
                <cfscript>
                    daysAgo = dateDiff("d", publishedDate, now());
                </cfscript>
                <cfif daysAgo LTE 7>
                    <span class="badge-new">NEW</span>
                </cfif>
                
                <a href="post.cfm?id=#postID#">Read more</a>
            </article>
        </cfoutput>
    </div>
</body>
</html>

Report generation with standardized date formats

Generate automated reports with consistent date formatting in filenames and content. This is important because:
  • Automated reports need predictable naming
  • Facilitates file organization and retrieval
  • Essential for archival and compliance
Organizations generate millions of reports annually. Without standardized date-based naming conventions, these files become impossible to manage. Fortune 500 companies report saving 200+ hours monthly by implementing consistent date-based report naming.
<cfscript>
    param name="url.startDate" type="date" default=dateAdd('d', -30, now());
    param name="url.endDate" type="date" default=now();
    
    // Query sales data
    qSales = queryExecute("
        SELECT 
            DATE(orderDate) as saleDate,
            COUNT(orderID) as orderCount,
            SUM(totalAmount) as dailyTotal
        FROM orders
        WHERE orderDate BETWEEN :startDate AND :endDate
        GROUP BY DATE(orderDate)
        ORDER BY saleDate
    ", {
        startDate: {value: url.startDate, cfsqltype: "cf_sql_date"},
        endDate: {value: url.endDate, cfsqltype: "cf_sql_date"}
    }, {
        datasource: "reporting"
    });
    
    // Calculate totals
    qTotals = queryExecute("
        SELECT 
            SUM(orderCount) as totalOrders,
            SUM(dailyTotal) as grandTotal
        FROM qSales
    ", {}, {
        dbtype: "query"
    });
    
    // Generate filename with timestamp
    reportDate = dateFormat(now(), "yyyy-mm-dd");
    reportTime = timeFormat(now(), "HHmmss");
    fileName = "sales_report_#reportDate#_#reportTime#.pdf";
    filePath = expandPath("./reports/#fileName#");
    
    // Create reports directory if it doesn't exist
    reportsDir = expandPath("./reports");
    if (!directoryExists(reportsDir)) {
        directoryCreate(reportsDir);
    }
</cfscript>

<!--- Generate PDF --->
<cfdocument format="pdf" filename="#filePath#" overwrite="true" margintop="0.5" marginbottom="0.5">
    <html>
    <head>
        <style>
            body { font-family: Arial, sans-serif; margin: 40px; }
            h1 { color: #333; text-align: center; }
            .header { text-align: center; margin-bottom: 30px; border-bottom: 2px solid #333; padding-bottom: 20px; }
            table { width: 100%; border-collapse: collapse; margin: 20px 0; }
            th { background-color: #4CAF50; color: white; padding: 12px; text-align: left; }
            td { padding: 10px; border-bottom: 1px solid #ddd; }
            tr:hover { background-color: #f5f5f5; }
            .summary { background-color: #f0f0f0; padding: 15px; margin-top: 20px; font-size: 14px; }
            .right-align { text-align: right; }
        </style>
    </head>
    <body>
        <cfoutput>
        <div class="header">
            <h1>Sales Report</h1>
            <p><strong>Report Period:</strong> #dateFormat(url.startDate, "mmmm d, yyyy")# - #dateFormat(url.endDate, "mmmm d, yyyy")#</p>
            <p><strong>Generated:</strong> #dateFormat(now(), "mmmm d, yyyy")# at #timeFormat(now(), "h:mm tt")#</p>
        </div>
        
        <h2>Daily Sales Summary</h2>
        <table>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Day</th>
                    <th class="right-align">Orders</th>
                    <th class="right-align">Total Sales</th>
                </tr>
            </thead>
            <tbody>
                <cfloop query="qSales">
                <tr>
                    <td>#dateFormat(saleDate, "mm/dd/yyyy")#</td>
                    <td>#dateFormat(saleDate, "dddd")#</td>
                    <td class="right-align">#orderCount#</td>
                    <td class="right-align">#dollarFormat(dailyTotal)#</td>
                </tr>
                </cfloop>
            </tbody>
        </table>
        
        <div class="summary">
            <h3>Report Summary</h3>
            <p><strong>Total Orders:</strong> #qTotals.totalOrders#</p>
            <p><strong>Total Revenue:</strong> #dollarFormat(qTotals.grandTotal)#</p>
            <p><strong>Average Daily Sales:</strong> #dollarFormat(qTotals.grandTotal / qSales.recordCount)#</p>
            <p><strong>Days in Period:</strong> #qSales.recordCount#</p>
        </div>
        </cfoutput>
    </body>
    </html>
</cfdocument>

<!DOCTYPE html>
<html>
<body>
    <cfoutput>
        <h2>Report Generated Successfully</h2>
        <p><strong>File Name:</strong> #fileName#</p>
        <p><strong>Location:</strong> #filePath#</p>
        <p><a href="reports/#fileName#" target="_blank">📄 Download Report</a></p>
    </cfoutput>
</body>
</html>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page