View comments | RSS feed
Contents > CFML Reference > ColdFusion Functions > DateAdd PreviousNext

DateAdd

Adds units of time to a date.

A date/time object.

Date and time functions

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

DateConvert, DatePart, CreateTimeSpan

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

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)

date

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

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.

<!--- 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="cfsnippets">
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>

Contents > CFML Reference > ColdFusion Functions > DateAdd PreviousNext

ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6.1

Comments are no longer accepted for ColdFusion MX 6.1. ColdFusion 8 is the current version.

Comments


Adam Cameron said on Apr 15, 2004 at 4:49 PM :
It might be worthwhile to specify that the <em>number</em> argument takes only an <em>integer</em> value, not just a numeric. EG: you cannot add 1.5 days using dateAdd(), only whole units.
fgwenger said on Jan 26, 2005 at 8:16 AM :
DateAdd("d", 1, "12/30/2004") gives you a date of 12/31/2004
DateAdd("d", 1, "12/31/2004") gives you a date of 12/31/2004

I'm guessing that's a bug.
Greg Thomas said on Oct 21, 2005 at 3:45 AM :
There appears to be a bug in the DateAdd with weekdays. If you add five week days to a Friday date, you get a date on a Sunday! If you add six days to a Thursday, you get a Friday, as expected:

<cfoutput>

<cfset ThursdayDate = CreateDate(2005, 10, 20)>
<cfset FridayDate = CreateDate(2005, 10, 21)>

<cfset ThursdayPlusSix = DateAdd( "w", 6, ThursdayDate )>
<cfset FridayPlusFive = DateAdd( "w", 5, FridayDate )>

<P>Six weekdays after #LSDateFormat( ThursdayDate, "dddd" )# (#ThursdayDate#) is #LSDateFormat( ThursdayPlusSix, "dddd" )# (#ThursdayPlusSix#)
<P>Five weekdays after #LSDateFormat( FridayDate, "dddd" )# (#FridayDate#) is #LSDateFormat( FridayPlusFive, "dddd" )# (#FridayPlusFive#)

</cfoutput>
ASandstrom said on Oct 21, 2005 at 7:29 AM :
I have entered this as a bug (#61361).

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/functi55.htm