compareTo 方法
- DateTime other
override
比较此 DateTime 对象与 other
,如果值相等则返回零。
compareTo 函数返回:
- 如果此 DateTime isBefore
other
,则返回负值。 - 如果此 DateTime isAtSameMomentAs
other
,则返回0
, - 否则返回正值(当此 DateTime isAfter
other
时)。
final now = DateTime.now();
final future = now.add(const Duration(days: 2));
final past = now.subtract(const Duration(days: 2));
final newDate = now.toUtc();
print(now.compareTo(future)); // -1
print(now.compareTo(past)); // 1
print(now.compareTo(newDate)); // 0
实现
external int compareTo(DateTime other);