difference method Null safety

Duration difference (
  1. DateTime other
)

Returns a Duration with the difference when subtracting other from this.

The returned Duration will be negative if other occurs after this.

var berlinWallFell = DateTime.utc(1989, DateTime.november, 9);
var dDay = DateTime.utc(1944, DateTime.june, 6);

Duration difference = berlinWallFell.difference(dDay);
assert(difference.inDays == 16592);

The difference is measured in seconds and fractions of seconds. The difference above counts the number of fractional seconds between midnight at the beginning of those dates. If the dates above had been in local time, not UTC, then the difference between two midnights may not be a multiple of 24 hours due to daylight saving differences.

For example, in Australia, similar code using local time instead of UTC:

var berlinWallFell = DateTime(1989, DateTime.november, 9);
var dDay = DateTime(1944, DateTime.june, 6);
Duration difference = berlinWallFell.difference(dDay);
assert(difference.inDays == 16592);

will fail because the difference is actually 16591 days and 23 hours, and Duration.inDays only returns the number of whole days.

Implementation

external Duration difference(DateTime other);