isBetween method
Checks if this DateTime falls between two other DateTime objects, with options for inclusive or exclusive boundaries and timezone handling.
start and end are the boundary DateTime objects.
inclusiveStart determines if the start date is included in the
range (defaults to true).
inclusiveEnd determines if the end date is included in the range
(defaults to false).
ignoreTime if true, only compares the date part, ignoring time
components (defaults to false).
normalize if true, converts all dates to UTC before comparison
(defaults to false).
Throws ArgumentError if:
- Either
startorendis null startdate is afterenddate
Implementation
bool isBetween(
DateTime start,
DateTime end, {
bool inclusiveStart = true,
bool inclusiveEnd = false,
bool ignoreTime = false,
bool normalize = false,
}) {
// Validate inputs
ArgumentError.checkNotNull(start, 'start');
ArgumentError.checkNotNull(end, 'end');
// Prepare dates for comparison
var self = this;
var compareStart = start;
var compareEnd = end;
// Normalize to UTC if requested so all three dates live in the same clock.
if (normalize) {
self = self.toUtc();
compareStart = start.toUtc();
compareEnd = end.toUtc();
}
// Strip time components if requested while preserving UTC state.
if (ignoreTime) {
self = _dateOnly(self);
compareStart = _dateOnly(compareStart);
compareEnd = _dateOnly(compareEnd);
}
// Validate range
if (compareStart.isAfter(compareEnd)) {
throw ArgumentError(
'Start date ($compareStart) must be before or equal to end date ($compareEnd)',
);
}
// Perform comparison
return (inclusiveStart
? !self.isBefore(compareStart)
: self.isAfter(compareStart)) &&
(inclusiveEnd ? !self.isAfter(compareEnd) : self.isBefore(compareEnd));
}