Dart/Flutter - Calculate Difference Between Two DateTimes

Find out how to calculate the difference between two DateTimes in Dart in this tutorial.

If you use Dart including any Dart framework such as Flutter, you can use the DateTime class two store values that represent times. Sometimes, it's necessary to calculate the difference between two DateTimes. This tutorial shows you how to get the difference between two DateTimes in Dart, along with the explanation.

In this tutorial, we are going to use the variables below. dt1 is earlier than dt2, while dt3 has the same time as dt1.

  final dt1 = DateTime.parse('2023-01-22 12:21:00');
  final dt2 = DateTime.parse('2023-01-23 14:26:00');
  final dt3 = DateTime.parse('2023-01-22 12:21:00');

Using difference

Dart's DateTime class has a method named difference.

  Duration difference(DateTime other)

It has an argument where you need to pass another DateTime. The method returns a Duration as the result. The returned value is the result of subtracting the passed DateTime (other) from the DateTime instance who calls the method. The time difference is measured in seconds and fractions of seconds.

Below are the examples along with the results.

  Duration diff1 = dt1.difference(dt2);
  print('Difference: $diff1'); // -26:05:00.000000

  Duration diff2 = dt2.difference(dt1);
  print('Difference: $diff2'); // 26:05:00.000000

  Duration diff3 = dt1.difference(dt3);
  print('Difference: $diff3'); // 0:00:00.000000

In the first example, the result is negative because dt2 is after dt1. In the second example, the result is positive because dt1 is before dt2. In the third example, the result is zero because dt1 and dt2 have the same time.

Calculation with Different Time Zones

What happens if we try to get the difference of two DateTimes with different time zones.

  DateTime utcDateTime = DateTime.utc(2023, 1, 1, 0, 0, 0);
  DateTime localDateTime = DateTime(2023, 1, 1, 0, 0, 0);
  DateTime localDateTime2 = DateTime(2023, 1, 1, 7, 0, 0);
  Duration diff1 = utcDateTime.difference(localDateTime);
  print('Difference: $diff1'); // 7:00:00.000000
  Duration diff2 = utcDateTime.difference(localDateTime2);
  print('Difference: $diff2'); // 0:00:00.000000

It turns out that Dart takes the time zone for calculation. The calculated difference is based on the time since epoch of each DateTime. As a result, the utcDateTime above has a time difference with localDateTime despite those two having the same local time. On the other hand, utcDateTime and localDateTime2 doesn't have any time difference because both times are at the same moment.

Some countries have different daylight savings which may affect the result.

For example, below are two DateTimes that represent two dates at 0 o'clock. If the times are in UTC or if the area doesn't have daylight saving time, the result will be exactly 16155 days, which is a multiple of 24 hours.

  final berlinWallFell = DateTime.utc(1989, DateTime.november, 9);
  final indonesiaIndependence = DateTime.utc(1945, DateTime.august, 17);
  final difference = berlinWallFell.difference(indonesiaIndependence); // 16155 days

However, despite the two DateTimes being set at 0 o'clock, the result may not be a multiple of 24 hours in some areas. For example, if the system uses the Australian timezone and the DateTimes' time zone is not set to UTC (which means using the local time zone). For the case above, the counted difference will be 16154 days and 23 hours due to daylight saving difference.

  final difference = berlinWallFell.difference(indonesiaIndependence); // 16154 days 23 hours

Summary

Calculating the difference between two DateTimes in Dart is quite simple as you can use the difference method. However, you need to be aware of daylight saving differences between time zones.

You can also read about: