Name

DateAdd

Synopsis

                  
                  DateAdd(datepart, number, date)

Adds number to the datepart of the specified date. Dates must be in the range AD 100-9999. To subtract, make number negative. Valid entries for datepart are l (millisecond), s (second), n (minute), h (hour), ww (week), w (weekday), d (day), y (day of year), m (month), q (quarter), and yyyy (year). For information on how ColdFusion MX handles two-digit years, see CreateDate( ). Examples:

<cfset MyDateTime = Now( )>
   
<cfoutput>The original time and date is 
#TimeFormat(MyDateTime,'hh:mm:ss tt')#, #DateFormat(MyDateTime,'mmmm dd, yyyy')#
   
<p><b>Add 30 Seconds:</b> 
#TimeFormat(DateAdd('s', 30, MyDateTime),'hh:mm:ss tt')#
<br><b>Subtract 10 minutes:</b> 
#TimeFormat(DateAdd('n', -10, MyDateTime),'hh:mm:ss tt')#
<br><b>Add 2 hours:</b> 
#TimeFormat(DateAdd('h', 2, MyDateTime),'hh:mm:ss tt')#
<br><b>Add 9 weeks:</b> 
#DateFormat(DateAdd('ww', 9, MyDateTime),'mmmm dd, yyyy')#
<br><b>Add 3 weekdays:</b> 
#DateFormat(DateAdd('w', 3, MyDateTime),'mmmm dd, yyyy')#
<br><b>Subtract 67 days:</b> 
#DateFormat(DateAdd('d', -67, MyDateTime),'mmmm dd, yyyy')#
<br><b>Add 45 days of the year:</b> 
#DateFormat(DateAdd('y', 45, MyDateTime),'mmmm dd, yyyy')#
<br><b>Add 7 months:</b> 
#DateFormat(DateAdd('m', 7, MyDateTime),'mmmm dd, yyyy')#
<br><b>Add 2 quarters:</b> 
#DateFormat(DateAdd('q', 2, MyDateTime),'mmmm dd, yyyy')#
<br><b>Add 5 years:</b> 
#DateFormat(DateAdd('yyyy', 5, MyDateTime),'mmmm dd, yyyy')#
</cfoutput>

Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.