Comparing only dates of DateTimes in Dart

后端 未结 4 1444
予麋鹿
予麋鹿 2020-12-29 23:56

I need to store and compare dates (without times) in my app, without caring about time zones.
I can see three solutions to this:

  1. (date1.year == da

4条回答
  •  一整个雨季
    2020-12-30 00:16

    I am using this function to calculate the difference in days.

    Comparing dates is tricky as the result depends not just on the timestamps but also the timezone of the user.

    int diffInDays (DateTime date1, DateTime date2) {
        return ((date1.difference(date2) - Duration(hours: date1.hour) + Duration(hours: date2.hour)).inHours / 24).round();
    }
    

提交回复
热议问题