Whatever message this page gives is out now! Go check it out!
DateFormat(date [, mask])Parameter | Description |
date | Date/time object, in the range 100 AD-9999 AD. |
mask | Characters that show how ColdFusion displays a date:
|
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. |
<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><cfscript>
writeOutput("The date is: " & DateFormat('04/10/2019','mm-dd'))
</cfscript><cfscript>
// This snippet throws an exception
writeOutput("The date is: " & DateFormat('04/10','mm-dd'))
</cfscript><cfscript>
writeOutput(dateformat(now(), "mm-D-yyyy") & "<br/>")
writeOutput(dateformat(now(), "mm-d-yyyy") & "<br/>")
</cfscript><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>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");
}
}
}<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><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><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><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>