DateAdd

Description

Returns a date to which a time interval has been added.

Category

Date and time functions

Syntax


DateAdd(datepart, number, date) 

See also

DateConvert, DatePart, CreateTimeSpan

Parameters

Parameter
Description
datepart
One of the following strings:
  • 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. Year values 0-29 are interpreted as 21st century dates. Year values 30-99 are interpreted as 20th century dates.

Usage

The datepart specifiers "y," "d," and "w" perform the same function    add a number of days to a date.

When passing a date/time value as a string, enclose it in quotes. Otherwise, it is interpreted as a number 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 will expire. (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>

  <TD>#Subject#</TD>

  <TD>#Posted# <cfif DateAdd(type, value, 

    posted) LT Now()>EXPIRED</cfif></TD>

</TR>

</cfoutput>

</TABLE>



Banner.Novgorod.Ru