You want to add to or subtract from a date/time object.
The DateAdd() function can be used to add or subtract various units of time from a date/time object. For example, you can use DateAdd() to add or subtract an arbitrary number of seconds/minutes/days/months, etc. to a specific date/time. DateAdd() takes the form:
DateAdd(datepart, number, date)
Valid entries for datepart are: s (second), n (minute), h (hour), ww (week), w (weekday), d (day), y (day of year), m (month), q (quarter), and yyyy (year). Number specifies the number of datepart units to add to date. To subtract from the specified date, make number negative. Here are some 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>Subtract 7 months:</b>
#DateFormat(DateAdd('m', -7, MyDateTime),'mmmm dd, yyyy')#
<br><b>Subtract 2 quarters:</b>
#DateFormat(DateAdd('q', -2, MyDateTime),'mmmm dd, yyyy')#
<br><b>Subtract 5 years:</b>
#DateFormat(DateAdd('yyyy', -5, MyDateTime),'mmmm dd, yyyy')#
</cfoutput>
This question was written by Rob Brooks-Bilson
It was last updated on January 6, 2006.