ColdFusion DateAdd() - How to add year, quarter, month, week, day, hour, minute, second to a date

DateAdd() - add year, quarter, month, week, day, hour, minute, second to a date

DateAdd.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DateAdd function example: how to add year, quarter, month, week, day, hour, minute, second to a date</title>
</head>

<body>
<h2 style="color:hotpink">DateAdd Function Example</h2>
<cfset MyDateTime=Now()>
<cfoutput>
<b>
MyDateTime: #MyDateTime#
<br />
MyDateTime: #DateFormat(MyDateTime)#
</b>
<br /><br />
</cfoutput>

<table style="color:LightGreen; background:Green;" border="1" bordercolor="DarkGreen">
    <tr>
     <td style="font-weight:bold">DateAdd</td>
     <td style="font-weight:bold">DatePart</td>
     <td style="font-weight:bold">Output</td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("yyyy",1,MyDateTime))#</td>
     <td>Year</td>
     <td><cfoutput>#DateFormat(DateAdd("yyyy",1,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("q",1,MyDateTime))#</td>
     <td>Quarter</td>
     <td><cfoutput>#DateFormat(DateAdd("q",1,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("m",1,MyDateTime))#</td>
     <td>Month</td>
     <td><cfoutput>#DateFormat(DateAdd("m",1,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("y",1,MyDateTime))#</td>
     <td>Day of year</td>
     <td><cfoutput>#DateFormat(DateAdd("y",1,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("w",2,MyDateTime))#</td>
     <td>Weekday</td>
     <td><cfoutput>#DateFormat(DateAdd("w",2,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateFormat(DateAdd("ww",1,MyDateTime))#</td>
     <td>Week</td>
     <td><cfoutput>#DateFormat(DateAdd("ww",1,MyDateTime))#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateAdd("h",1,MyDateTime)#</td>
     <td>Hour</td>
     <td><cfoutput>#DateAdd("h",1,MyDateTime)#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateAdd("n",1,MyDateTime)#</td>
     <td>Minute</td>
     <td><cfoutput>#DateAdd("n",1,MyDateTime)#</cfoutput></td>
    </tr>
    <tr>
     <td>#DateAdd("s",10,MyDateTime)#</td>
     <td>Second</td>
     <td><cfoutput>#DateAdd("s",10,MyDateTime)#</cfoutput></td>
    </tr>
</table>

</body>
</html>





More ColdFusion examples