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

ParameterOptional/RequiredDescription
otherrequiredthe 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,

  1. We create two DateTime objects, dt1 and dt2, representing different dates.
  2. We then use the isAfter() method on dt1 with dt2 as the argument.
  3. The method returns true since dt1 occurs after dt2.
  4. 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,

  1. We create two identical DateTime objects, dt1 and dt2, representing the same date.
  2. We then use the isAfter() method on dt1 with dt2 as the argument.
  3. The method returns false since dt1 does not occur after dt2.
  4. 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,

  1. We create two DateTime objects, dt1 and dt2, representing different dates.
  2. We then use the isAfter() method on dt1 with dt2 as the argument.
  3. The method returns false since dt1 does not occur after dt2.
  4. 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.