Whatever message this page gives is out now! Go check it out!

DateAdd

Last update:
May 18, 2026

Description

Adds units of time to a date.

Returns

A date/time object.

Category

Function syntax

DateAdd(datepart, number, date)

See also

History

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

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
  • 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). Number must be an integer.
date
Date/time object, in the range 100 AD-9999 AD.

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, enclose it in quotation marks. Otherwise, it is interpreted as a numeric representation of a date/time object.

Example

<cfscript>
    date="{ts '2433-09-01 23:59:59'}";
 writeOutput("#date#");
    diff=30;
    posdateresult=DateAdd("s",diff,date);
 writeOutput("#posdateresult#");
</cfscript>
Output
{ts '2433-09-01 23:59:59'}{ts '2433-09-02 00:00:29'}

Real-world use cases of using the DateAdd function

eCommerce subscription renewal dates

Calculate subscription renewal dates for different plan types. Automatically set renewal dates when customers purchase subscriptions.
<cfscript>
    writeOutput("<h2>1. E-Commerce Subscription Renewal System</h2>");
    
    // Customer subscription start date
    subscriptionStart = now();
    
    // Different subscription types with their renewal periods
    subscriptionPlans = [
        {name: "Monthly Premium", period: "m", duration: 1},
        {name: "Quarterly Business", period: "q", duration: 1},
        {name: "Annual Enterprise", period: "yyyy", duration: 1},
        {name: "Weekly Trial", period: "ww", duration: 1}
    ];
    
    writeOutput("<p><strong>Subscription Start Date:</strong> " & dateFormat(subscriptionStart, "mm/dd/yyyy") & " " & timeFormat(subscriptionStart, "HH:mm:ss") & "</p>");
    writeOutput("<table border='1' cellpadding='5'>");
    writeOutput("<tr><th>Plan Type</th><th>Duration</th><th>Renewal Date</th><th>Days Until Renewal</th></tr>");
    
    for (plan in subscriptionPlans) {
        renewalDate = dateAdd(plan.period, plan.duration, subscriptionStart);
        daysUntilRenewal = dateDiff("d", subscriptionStart, renewalDate);
        
        writeOutput("<tr>");
        writeOutput("<td>" & plan.name & "</td>");
        writeOutput("<td>" & plan.duration & " " & plan.period & "</td>");
        writeOutput("<td>" & dateFormat(renewalDate, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>" & daysUntilRenewal & " days</td>");
        writeOutput("</tr>");
    }
    writeOutput("</table><br>");
    
    // Calculate grace period (7 days after renewal)
    monthlyRenewal = dateAdd("m", 1, subscriptionStart);
    gracePeriodEnd = dateAdd("d", 7, monthlyRenewal);
    writeOutput("<p><strong>Grace Period Example:</strong> Monthly renewal on " & dateFormat(monthlyRenewal, "mm/dd/yyyy") & " with grace period ending " & dateFormat(gracePeriodEnd, "mm/dd/yyyy") & "</p>");
</cfscript>

Project management- Calculate deadlines

Calculate project milestones and deadlines based on business days. Automatically set project timelines excluding weekends.
<cfscript>
    writeOutput("<hr><h2>2. Project Management Timeline Calculator</h2>");
    
    projectStartDate = createDate(2024, 1, 15); // Monday
    
    // Project phases with business day durations
    projectPhases = [
        {phase: "Requirements Gathering", businessDays: 5},
        {phase: "Design & Architecture", businessDays: 10}, 
        {phase: "Development Sprint 1", businessDays: 15},
        {phase: "Development Sprint 2", businessDays: 15},
        {phase: "Testing & QA", businessDays: 8},
        {phase: "Deployment & Launch", businessDays: 3}
    ];
    
    writeOutput("<p><strong>Project Start Date:</strong> " & dateFormat(projectStartDate, "mm/dd/yyyy") & " (" & dayOfWeekAsString(dayOfWeek(projectStartDate)) & ")</p>");
    writeOutput("<table border='1' cellpadding='5'>");
    writeOutput("<tr><th>Phase</th><th>Business Days</th><th>Start Date</th><th>End Date</th><th>Day of Week</th></tr>");
    
    currentDate = projectStartDate;
    
    for (phase in projectPhases) {
        phaseStart = currentDate;
        
        // Add business days (multiply by 1.4 to account for weekends approximately)
        // More precise method would check each day, but this gives a good estimate
        totalDays = ceiling(phase.businessDays * 1.4);
        phaseEnd = dateAdd("d", totalDays, phaseStart);
        
        // Adjust if it falls on weekend
        while (dayOfWeek(phaseEnd) == 1 OR dayOfWeek(phaseEnd) == 7) {
            phaseEnd = dateAdd("d", 1, phaseEnd);
        }
        
        writeOutput("<tr>");
        writeOutput("<td>" & phase.phase & "</td>");
        writeOutput("<td>" & phase.businessDays & "</td>");
        writeOutput("<td>" & dateFormat(phaseStart, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>" & dateFormat(phaseEnd, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>" & dayOfWeekAsString(dayOfWeek(phaseEnd)) & "</td>");
        writeOutput("</tr>");
        
        currentDate = dateAdd("d", 1, phaseEnd); // Next phase starts day after
    }
    writeOutput("</table><br>");
    
    // Calculate project completion
    finalDate = dateAdd("d", -1, currentDate);
    projectDuration = dateDiff("d", projectStartDate, finalDate);
    writeOutput("<p><strong>Total Project Duration:</strong> " & projectDuration & " calendar days</p>");
</cfscript>

Financial system- Payment scheduling

Generate payment schedules for loans, installments, and recurring bills. Calculate due dates for various payment frequencies.
<cfscript>
    writeOutput("<hr><h2>3. Financial Payment Scheduling System</h2>");
    
    // Loan details
    loanAmount = 50000;
    loanStartDate = createDate(2024, 1, 1);
    monthlyPayment = 1250;
    
    writeOutput("<p><strong>Loan Details:</strong></p>");
    writeOutput("<ul>");
    writeOutput("<li>Loan Amount: " & dollarFormat(loanAmount) & "</li>");
    writeOutput("<li>Monthly Payment: " & dollarFormat(monthlyPayment) & "</li>");
    writeOutput("<li>Start Date: " & dateFormat(loanStartDate, "mm/dd/yyyy") & "</li>");
    writeOutput("</ul>");
    
    writeOutput("<h3>Payment Schedule (First 12 months)</h3>");
    writeOutput("<table border='1' cellpadding='5'>");
    
    for (i = 1; i <= 12; i++) {
        paymentDate = dateAdd("m", i, loanStartDate);
        daysFromStart = dateDiff("d", loanStartDate, paymentDate);
        
        writeOutput("<tr>");
        writeOutput("<td>" & i & "</td>");
        writeOutput("<td>" & dateFormat(paymentDate, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>$" & dollarFormat(monthlyPayment) & "</td>");
        writeOutput("<td>" & daysFromStart & "</td>");
        writeOutput("</tr>");
    }
    writeOutput("</table><br>");
    
    // Calculate different payment frequencies
    writeOutput("<h3>Alternative Payment Frequencies</h3>");
    paymentTypes = [
        {type: "Weekly", period: "ww", multiplier: 1, amount: 288},
        {type: "Bi-Weekly", period: "ww", multiplier: 2, amount: 577},
        {type: "Quarterly", period: "q", multiplier: 1, amount: 3750},
        {type: "Semi-Annual", period: "m", multiplier: 6, amount: 7500}
    ];
    
    writeOutput("<table border='1' cellpadding='5'>");
    writeOutput("<tr><th>Payment Type</th><th>Next Due Date</th><th>Amount</th><th>Following Date</th></tr>");
    
    for (payment in paymentTypes) {
        nextDue = dateAdd(payment.period, payment.multiplier, loanStartDate);
        followingDue = dateAdd(payment.period, payment.multiplier, nextDue);
        
        writeOutput("<tr>");
        writeOutput("<td>" & payment.type & "</td>");
        writeOutput("<td>" & dateFormat(nextDue, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>$" & dollarFormat(payment.amount) & "</td>");
        writeOutput("<td>" & dateFormat(followingDue, "mm/dd/yyyy") & "</td>");
        writeOutput("</tr>");
    }
    writeOutput("</table><br>");
</cfscript>

Event management- Scheduling and reminders

Event planning system with automated reminder scheduling. Calculate reminder dates and event scheduling for different time zones.
<cfscript>
    writeOutput("<hr><h2>4. Event Management & Reminder System</h2>");
    
    // Main event details
    eventDate = createDateTime(2024, 6, 15, 14, 30, 0); // June 15, 2024, 2:30 PM
    eventName = "Annual Company Conference";
    
    writeOutput("<p><strong>Event:</strong> " & eventName & "</p>");
    writeOutput("<p><strong>Event Date & Time:</strong> " & dateFormat(eventDate, "mm/dd/yyyy") & " at " & timeFormat(eventDate, "h:mm tt") & "</p>");
    
    // Calculate reminder schedule
    reminderSchedule = [
        {name: "Save the Date", days: -90, timeUnit: "d"},
        {name: "Early Bird Registration", days: -60, timeUnit: "d"},
        {name: "Final Registration Reminder", days: -30, timeUnit: "d"},
        {name: "One Week Notice", days: -7, timeUnit: "d"},
        {name: "Day Before Reminder", days: -1, timeUnit: "d"},
        {name: "Day of Event - Morning", hours: -4, timeUnit: "h"},
        {name: "Event Starts Soon", minutes: -30, timeUnit: "n"}
    ];
    
    writeOutput("<h3>Automated Reminder Schedule</h3>");
    writeOutput("<table border='1' cellpadding='5'>");
    writeOutput("<tr><th>Reminder Type</th><th>Send Date</th><th>Send Time</th><th>Days Until Event</th></tr>");
    
    for (reminder in reminderSchedule) {
        if (structKeyExists(reminder, "days")) {
            reminderDate = dateAdd(reminder.timeUnit, reminder.days, eventDate);
        } else if (structKeyExists(reminder, "hours")) {
            reminderDate = dateAdd(reminder.timeUnit, reminder.hours, eventDate);
        } else if (structKeyExists(reminder, "minutes")) {
            reminderDate = dateAdd(reminder.timeUnit, reminder.minutes, eventDate);
        }
        
        daysUntilEvent = dateDiff("d", reminderDate, eventDate);
        
        writeOutput("<tr>");
        writeOutput("<td>" & reminder.name & "</td>");
        writeOutput("<td>" & dateFormat(reminderDate, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>" & timeFormat(reminderDate, "h:mm tt") & "</td>");
        writeOutput("<td>" & daysUntilEvent & "</td>");
        writeOutput("</tr>");
    }
    writeOutput("</table><br>");
    
    // Multi-day event scheduling
    writeOutput("<h3>Multi-Day Conference Schedule</h3>");
    conferenceDays = [
        {day: "Pre-Conference Workshop", dayOffset: -1, startTime: "09:00"},
        {day: "Main Conference Day 1", dayOffset: 0, startTime: "08:30"},
        {day: "Main Conference Day 2", dayOffset: 1, startTime: "08:30"},
        {day: "Post-Conference Networking", dayOffset: 2, startTime: "18:00"}
    ];
    
    writeOutput("<table border='1' cellpadding='5'>");
    writeOutput("<tr><th>Event Day</th><th>Date</th><th>Start Time</th><th>Day of Week</th></tr>");
    
    for (day in conferenceDays) {
        dayDate = dateAdd("d", day.dayOffset, eventDate);
        
        writeOutput("<tr>");
        writeOutput("<td>" & day.day & "</td>");
        writeOutput("<td>" & dateFormat(dayDate, "mm/dd/yyyy") & "</td>");
        writeOutput("<td>" & day.startTime & "</td>");
        writeOutput("<td>" & dayOfWeekAsString(dayOfWeek(dayDate)) & "</td>");
        writeOutput("</tr>");
    }
    writeOutput("</table><br>");
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page