Skip to main content Link Search Menu Expand Document Toggle dark mode Copy Code (external link)

addHours() : Add Hours to Date

Add hours to a datetime. Use negative values to subtract hours.


On this page

Signature

addHours(date, hours)

Parameters

Parameter Type Required Description
date DateTime Yes The datetime to modify
hours Number Yes Number of hours to add (can be negative)

Returns

Type: DateTime

A new DateTime with the specified hours added.


Examples

Add Hours

<p>Meeting ends: {{format(addHours(model.startTime, 2), 'h:mm tt')}}</p>

Data:

doc.Params["model"] = new {
    startTime = new DateTime(2024, 3, 15, 14, 30, 0)
};

Output:

<p>Meeting ends: 4:30 PM</p>

Delivery Window

<p>Delivery between {{format(addHours(model.orderTime, 2), 'h:mm tt')}} and {{format(addHours(model.orderTime, 4), 'h:mm tt')}}</p>

Data:

doc.Params["model"] = new {
    orderTime = new DateTime(2024, 3, 15, 10, 0, 0)
};

Output:

<p>Delivery between 12:00 PM and 2:00 PM</p>

Time Zone Adjustment

<p>EST: {{format(model.time, 'h:mm tt')}}</p>
<p>PST: {{format(addHours(model.time, -3), 'h:mm tt')}}</p>

Notes

  • Input datetime is not modified (returns new datetime)
  • Can add positive or negative hours
  • Automatically handles day boundaries
  • Use addMinutes() for minute increments
  • Use addDays() for day increments

See Also