DateAdd

Description

Adds units of time to a date.

Return value

A date/time object.

Category

Date and time functions

Syntax

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

See also

DateConvert, DatePart, CreateTimeSpan

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
number
Number of units of datepart to add to date (positive, to get dates in the future; negative, to get dates in the past)
date
Date/time object, in the range 100 AD-9999 AD. See "How ColdFusion processes two-digit year values".

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, you must 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 --->
...
<cfquery name = "GetMessages" datasource = "cfsnippets">
SELECT UserName, Subject, Posted
FROM Messages
</cfquery>
<p>This example uses DateAdd to determine when a message in the database 
expires. (The value selected is messages older than <cfoutput>#value#
<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>
</cfoutput>).

<TABLE>
<TR>
  <TD>UserName</TD>
  <TD>Subject</TD>
  <TD>Posted</TD>
</TR>
<cfoutput query = "GetMessages">
<TR>
  <TD>#UserName#</td>
  <d>#Subject#</d>
  <d>#Posted# <cfif DateAdd(type, value, posted) 
LT Now()>EXPIRED</cfif></d>
</tr>
</cfoutput>
</table>

Comments