This tutorial shows you several ways to compare two DateTime
s in Dart, including any Dart framework such as Flutter. Dart has DateTime
class, which is usually used to store date time values. If you want to compare two DateTime
s, there are several built-in methods that you can use. Below are the examples.
In this tutorial, we have three DateTime
variables. dt1
is earlier than dt2
, while dt3
has the same time as dt1
.
final dt1 = DateTime.parse('2023-01-23 14:21:00');
final dt2 = DateTime.parse('2023-01-23 14:26:00');
final dt3 = DateTime.parse('2023-01-23 14:21:00');
Using compareTo
The first method you can use is comapreTo
.
int compareTo(DateTime other)
It returns an integer that indicates the comparison result between a DateTime
instance who calls the method and another DateTime
passed as the argument. The result is negative if the DateTime
instance who calls the method is before the passed DateTime
. On the contrary, it returns a positive integer if the passed DateTime
is earlier. If both DateTime
s have the same time, it will return 0.
Examples:
print(dt1.compareTo(dt2)); // -1
print(dt2.compareTo(dt1)); // 1
print(dt1.compareTo(dt3)); // 0
In the first example, it returns -1 because dt1
is earlier than dt2
. In the second example, it returns 1 because dt2
occurs after dt1
. In the third example, it returns 0 because dt1
and dt2
have the same time.
Using isAfter
To check whether a DateTime
is after another one, you can use the isAfter
method.
bool isAfter(DateTime other)
Examples:
print(dt1.isAfter(dt2)); // false
print(dt2.isAfter(dt1)); // true
print(dt1.isAfter(dt3)); // false
In the first example, the result is false because dt1
is not after dt2
. In the second example, the result is true because dt2
is after dt1
. In the third example, the result is false because dt1
is not after dt3
.
Using isBefore
To check whether a DateTime
is before another one, you can use the isBefore
method.
bool isBefore(DateTime other)
Examples:
print(dt1.isBefore(dt2)); // true
print(dt2.isBefore(dt1)); // false
print(dt1.isBefore(dt3)); // false
In the first example, the result is true because dt1
is before dt2
. In the second example, the result is false because dt2
is not before dt1
. In the third example, the result is false because dt1
is not before dt3
.
Using isAtSameMomentAs
To compare that two DateTime
s have the same time, you can use isAtSameMomentAs
. Examples:
bool isAtSameMomentAs(DateTime other)
print(dt1.isAtSameMomentAs(dt2)); // false
print(dt1.isAtSameMomentAs(dt3)); // true
In the first example, the result is false because dt1
is before dt2
. In the second example, the result is true because dt1
and dt3
have the same time.
Using Equality Operator (==
)
The equality operator can be used to compare two DateTime
s. It will return true if both objects are at the same moment and in the same time zone. Let's assume the system date runs the following code using GMT+7 time zone. In the code below, both times are at the same moment but in different time zones. The equality operator returns false because the time zone is different.
DateTime utcDateTime = DateTime.utc(2023, 1, 1, 0, 0, 0);
DateTime localDateTime = DateTime(2023, 1, 1, 7, 0, 0);
print(utcDateTime == localDateTime); // false
In the example below, both times have the same time and time zone. Therefore, the result of the equality operator is true.
DateTime utcDateTime = DateTime.utc(2023, 1, 1, 0, 0, 0);
DateTime utcDateTime2 = DateTime.utc(2023, 1, 1, 0, 0, 0);
print(utcDateTime == utcDateTime2); // true
Comparison with Different Time Zones
If the two DateTime
s to be compared have different time zones, what's used by Dart for comparison is the time since epoch of each object.
For example, my local time is GMT+7. There are two DateTime
s, one in UTC and the other in local time. In the example below, both times have the same local time. However, actually both times are not at the same moment because the localDateTime
is before utcDateTime
. Therefore, Dart treats these two values as different.
DateTime utcDateTime = DateTime.utc(2023, 1, 1, 0, 0, 0);
DateTime localDateTime = DateTime(2023, 1, 1, 0, 0, 0); // in UTC, the time is 2022-12-31 17:00:00
print(utcDateTime.compareTo(localDateTime)); // 1
print(utcDateTime.isAfter(localDateTime)); // true
print(utcDateTime.isBefore(localDateTime)); // false
print(utcDateTime.isAtSameMomentAs(localDateTime)); // false
print(utcDateTime == localDateTime); // false
In the next example, despite the values having different local times, actually they are at the same moment. You can see the comparison results below.
DateTime utcDateTime = DateTime.utc(2023, 1, 1, 0, 0, 0);
DateTime localDateTime = DateTime(2023, 1, 1, 7, 0, 0); // in UTC, the time is 2023-1-1 00:00:00
print(utcDateTime.compareTo(localDateTime)); // 0
print(utcDateTime.isAfter(localDateTime)); // false
print(utcDateTime.isBefore(localDateTime)); // false
print(utcDateTime.isAtSameMomentAs(localDateTime)); // true
print(utcDateTime == localDateTime); // false
Summary
Comparing two DateTime
s in Dart can be done using one of the built-in methods which include compareTo
, isAfter
, isBefore
, isAtSameMomentAs
, and the equality operator. Just choose one that's best suited to the case.
You can also read about: