Dart DateTime isAfter()
Syntax & Examples
Syntax of DateTime.isAfter()
The syntax of DateTime.isAfter() method is:
bool isAfter(DateTime other) This isAfter() method of DateTime returns true if this occurs after other.
Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
other | required | the DateTime object to compare to |
Return Type
DateTime.isAfter() returns value of type bool.
✐ Examples
1 Check if the first date occurs after the second date
In this example,
- We create two DateTime objects,
dt1anddt2, representing different dates. - We then use the
isAfter()method ondt1withdt2as the argument. - The method returns
truesincedt1occurs afterdt2. - We print the result to standard output.
Dart Program
void main() {
var dt1 = DateTime(2024, 5, 13);
var dt2 = DateTime(2024, 5, 7);
var result = dt1.isAfter(dt2);
print(result);
}Output
true
2 Check if the first date occurs after the second date when they are equal
In this example,
- We create two identical DateTime objects,
dt1anddt2, representing the same date. - We then use the
isAfter()method ondt1withdt2as the argument. - The method returns
falsesincedt1does not occur afterdt2. - We print the result to standard output.
Dart Program
void main() {
var dt1 = DateTime(2024, 5, 3);
var dt2 = DateTime(2024, 5, 3);
var result = dt1.isAfter(dt2);
print(result);
}Output
false
3 Check if the first date occurs after the second date when the first date is earlier
In this example,
- We create two DateTime objects,
dt1anddt2, representing different dates. - We then use the
isAfter()method ondt1withdt2as the argument. - The method returns
falsesincedt1does not occur afterdt2. - We print the result to standard output.
Dart Program
void main() {
var dt1 = DateTime(2024, 5, 11);
var dt2 = DateTime(2024, 5, 15);
var result = dt1.isAfter(dt2);
print(result);
}Output
false
Summary
In this Dart tutorial, we learned about isAfter() method of DateTime: the syntax and few working examples with output and detailed explanation for each example.