DateAdd("datepart", number, "date")
DateConvert,
DatePart,
CreateTimeSpan
Parameter | Description |
---|---|
datepart |
String:
|
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". |
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 ---> ... <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>