给 DateTime
写一个 extension
:
extension DateTimeExtension on DateTime {
bool isSameDay(DateTime date) {
// ignore hour,minute,second..
final dateFormat = DateFormat("yyyy-MM-dd");
final date1 = dateFormat.format(this);
final date2 = dateFormat.format(date);
return date1 == date2;
}
}
使用:
final date1 = DateTime(2021, 12, 10, 01, 01, 01);
final date2 = DateTime(2021, 12, 10, 23, 59, 59);
print(date1.isSameDay(date2)); // true