😇
Flutter, Swift and Kotlin - Date(Time)型 同日判定
flutter
bool isSameDay(DateTime a, DateTime b) {
return a.year == b.year && a.month == b.month && a.day == b.day;
}
swift
func isSameDay(a: Date, b: Date) -> Bool {
return Calendar.current.isDate(a, equalTo: b, toGranularity: .day)
}
kotlin
// 文字列で比較するパターン
@SuppressLint("SimpleDateFormat")
fun isSameDay(a: Date, b: Date): Boolean {
val sdf = SimpleDateFormat("yyyyMMdd")
return sdf.format(a) == sdf.format(b)
}
// LocalDateに変換するパターン
fun isSameDay(a: Date, b: Date): Boolean {
val ald = a.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
val bld = b.toInstant().atZone(ZoneId.systemDefault()).toLocalDate()
return ald.year == bld.year && ald.dayOfYear == bld.dayOfYear
}
Discussion