'Check if one date is between two dates or not
I have the following three dates
Date1 = 2022-02-07 20:06:14.379392
Date2 = 2021-02-07 20:06:14.379393
Date3 = 2022-02-06 00:00:00.000
and I want to check, if Date3 is between Date1 and Date2.
As of right now the Date3 is between Date1 and Date2, but how can I check this?
Currently I am trying something like this
if (Date1.isBefore(Date3) &&
Date2.isAfter(Date3)) {
print("date3 is between date1 and date2");
} else {
print("date3 isn't between date1 and date2");
}
But it's always showing, that the Date3 is not between Date1 and Date2.
Solution 1:[1]
If you parse the dates as DateTime then fix the condition, it should work. Side note: you should rename your variables to something more explicit, ex. dateMin/dateMax, dateLowerBound/dateUpperBound, etc.
void main() {
final date1 = DateTime.parse('2022-02-07 20:06:14.379392');
final date2 = DateTime.parse('2021-02-07 20:06:14.379393');
final date3 = DateTime.parse('2022-02-06 00:00:00.000');
if (date3.isBefore(date1) && date3.isAfter(date2)) {
print("date3 is between date1 and date2"); // <- this is printed.
}
else {
print("date3 isn't between date1 and date2");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | evilmandarine |
