daysBetween() : Calculate Days Between Dates
On this page
Summary
Calculate the number of days between two dates. Returns a positive or negative number depending on whether the second date is after or before the first.
Signature
daysBetween(startDate, endDate)
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
startDate |
DateTime | Yes | The starting date |
endDate |
DateTime | Yes | The ending date |
Returns
Type: Number (Integer)
The number of days between the two dates. Positive if endDate is after startDate, negative if before.
Examples
Calculate Days Until Event
Data:
doc.Params["model"] = new {
today = new DateTime(2024, 3, 15),
launchDate = new DateTime(2024, 4, 20)
};
Output:
<p>Days until launch: 36</p>
Project Duration
Data:
doc.Params["model"] = new {
startDate = new DateTime(2024, 1, 15),
endDate = new DateTime(2024, 3, 30)
};
Output:
<h3>Project Timeline</h3>
<p>Start: January 15, 2024</p>
<p>End: March 30, 2024</p>
<p>Duration: 75 days</p>
Overdue Calculation
Data:
doc.Params["model"] = new {
today = new DateTime(2024, 3, 15),
tasks = new[] {
new { name = "Design", dueDate = new DateTime(2024, 3, 10) },
new { name = "Development", dueDate = new DateTime(2024, 3, 20) }
}
};
Output:
<p>Design: OVERDUE by 5 days</p>
<p>Development: Due in 5 days</p>
Notes
- Returns integer number of days (fractional days are truncated)
- Positive result: endDate is after startDate
- Negative result: endDate is before startDate
- Time component is ignored (only date part is used)
- For hour-level precision, use
hoursBetween() - For fractional days, calculate using
hoursBetween() / 24