TimeSpan & ranges
TimeSpan models an interval between two dates, with set operations and value equality. rangeTo and iterate step through a range; clamp, earliest and latest compare dates.
Set operations
LOADING…
⚙ TRY IT
⚡ OUTPUT
final a = TimeSpan(DateTime(2025, 3, 1), DateTime(2025, 3, 20));
final b = TimeSpan(DateTime(2025, 3, 10), DateTime(2025, 3, 31));
a.intersects(b);
a.merge(b);
a.getIntersection(b);
a.getDifferences(b);
a.symmetricDifference(b);
| Method | Returns |
|---|---|
intersects(other) | true when the spans share an instant, including containment |
merge(other) | the union; throws RangeError when they don't intersect |
getIntersection(other) | the overlap, or null |
getDifferences(other) | the parts not covered by other (0, 1 or 2 spans) |
symmetricDifference(other) | the parts covered by only one span |
Iterating
Steps are calendar steps: a month step from the 31st lands on the last day of shorter months, and day steps keep the wall-clock time across DST.
LOADING…
⚙ TRY IT
⚡ OUTPUT
DateTime(2025, 1, 31, 9).rangeTo(DateTime(2025, 6, 30, 9), unit: Unit.month, step: 1);
// or, on a TimeSpan
TimeSpan(start, end).iterate(unit: Unit.month, step: 1);
clamp, earliest, latest
LOADING…
⚙ TRY IT
⚡ OUTPUT
date.clamp(min, max);
[a, b, c].earliest;
[a, b, c].latest;