DateAdd

Description

Adds units of time to a date.

Returns

A date/time object.

Function syntax

DateAdd("datepart", number, "date")

History

ColdFusion MX 6.1: Added the datepart character L or l to represent milliseconds.

Parameters

Parameter

Description

datepart

String:

  • yyyy: Year

  • q: Quarter

  • m: Month

  • y: Day of year

  • d: Day

  • w: Weekday

  • ww: Week

  • h: Hour

  • n: Minute

  • s: Second

  • l: Millisecond

number

Number of units of datepart to add to date (positive, to get dates in the future; negative, to get dates in the past). Number must be an integer.

date

Date/time object, in the range 100 AD–9999 AD.

Usage

The datepart specifiers y, d, and w add a number of days to a date.

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.

Example

<!--- This example shows the use of DateAdd ---> 
<cfparam name="value" default="70"> 
<cfparam name="type" default="m"> 
 
<!--- If numbers passed, then use those. ---> 
<cfif IsDefined("form.value")> 
    <cfset value = form.value> 
</cfif> 
<cfif IsDefined("form.type")> 
    <cfset type = form.type> 
</cfif> 
 
 
<cfquery name="GetMessages" datasource="cfdocexamples"> 
    SELECT UserName, Subject, Posted 
    FROM Messages 
</cfquery> 
 
<p>This example uses DateAdd to determine when a message in 
the database will expire. Currently, messages older 
than <cfoutput>#value#</cfoutput> 
 
<cfswitch expression="#type#"> 
    <cfcase value="yyyy">years</cfcase> 
    <cfcase value="q">quarters</cfcase> 
    <cfcase value="m">months</cfcase> 
    <cfcase value="y">days of year</cfcase>     
    <cfcase value="w">weekdays</cfcase>     
    <cfcase value="ww">weeks</cfcase>     
    <cfcase value="h">hours</cfcase>     
    <cfcase value="n">minutes</cfcase>     
    <cfcase value="s">seconds</cfcase>         
    <cfdefaultcase>years</cfdefaultcase> 
</cfswitch> 
    are expired. 
 
<table> 
<tr> 
    <td>UserName</td> 
    <td>Subject</td> 
    <td>Posted</td> 
</tr> 
<cfoutput query="GetMessages"> 
<tr> 
    <td>#UserName#</td> 
    <td>#Subject#</td> 
    <td>#Posted# <cfif DateAdd(type, value, posted) LT Now()><font color="red">EXPIRED</font></cfif></td> 
</tr> 
</cfoutput> 
</table> 
 
<cfform action="#CGI.Script_Name#" method="post"> 
 
Select an expiration value: 
<cfinput type="Text" name="value" value="#value#" message="Please enter whole numbers only" validate="integer" required="Yes"> 
<select name="type"> 
    <option value="yyyy">years 
    <option value="m" selected>months 
    <option value="d">days 
    <option value="ww">weeks                 
    <option value="h">hours 
    <option value="n">minutes 
    <option value="s">seconds 
</select> 
     
<input type="Submit" value="Submit"> 
</cfform>