Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 296 Vote(s) - 3.67 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flutter: Find the number of days between two dates

#11
**The accepted answer is wrong. Don't use it.**

This is correct:

```dart
int daysBetween(DateTime from, DateTime to) {
from = DateTime(from.year, from.month, from.day);
to = DateTime(to.year, to.month, to.day);
return (to.difference(from).inHours / 24).round();
}
```

Testing:

```dart
DateTime date1 = DateTime.parse("2020-01-09 23:00:00.299871");
DateTime date2 = DateTime.parse("2020-01-10 00:00:00.299871");

expect(daysBetween(date1, date2), 1); // Works!
```

---

**Explanation why the accepted answer is wrong:**

Just run this:

```dart
int daysBetween_wrong1(DateTime date1, DateTime date2) {
return date1.difference(date2).inDays;
}

DateTime date1 = DateTime.parse("2020-01-09 23:00:00.299871");
DateTime date2 = DateTime.parse("2020-01-10 00:00:00.299871");

// Should return 1, but returns 0.
expect(daysBetween_wrong1(date1, date2), 0);
```

Note: Because of daylight savings, you can have a 23 hours difference between some day and the next day, even if you normalize to 0:00. That's why the following is ALSO incorrect:

```dart
// Fails, for example, when date2 was moved 1 hour before because of daylight savings.
int daysBetween_wrong2(DateTime date1, DateTime date2) {
from = DateTime(date1.year, date1.month, date1.day);
to = DateTime(date2.year, date2.month, date2.day);
return date2.difference(date1).inDays;
}
```

*Rant: If you ask me, Dart `DateTime` is very bad. It should at least have basic stuff like `daysBetween` and also timezone treatment etc.*

---

**Update:** The package

[To see links please register here]

claims to be a port of Noda Time. If that's the case, and it's ported correctly (I haven't tested it yet) then that's the Date/Time package you should probably use.
Reply

#12
The above answers are also correct, I just create a single method to find out the difference between the two days, accepted for the current day.

void differenceBetweenDays() {
final date1 = DateTime(2022, 01, 01); // 01 jan 2022
final date2 = DateTime(2022, 02, 01); // 01 feb 2022
final currentDay = DateTime.now(); // Current date
final differenceFormTwoDates = daysDifferenceBetween(date1, date2);
final differenceFormCurrent = daysDifferenceBetween(date1, currentDay);

print("difference From date1 and date 2 :- "+differenceFormTwoDates.toString()+" "+"Days");
print("difference From date1 and Today :- "+differenceFormCurrent.toString()+" "+"Days");

}
int daysDifferenceBetween(DateTime from, DateTime to) {
from = DateTime(from.year, from.month, from.day);
to = DateTime(to.year, to.month, to.day);
return (to.difference(from).inHours / 24).round();
}
Reply

#13
**Naively subtracting one `DateTime` from another with [`DateTime.difference`](

[To see links please register here]

) is subtly wrong.** As explained by [the `DateTime` documentation](

[To see links please register here]

):

> The difference between two dates in different time zones is just the number of nanoseconds between the two points in time. It doesn't take calendar days into account. That means that the difference between two midnights in local time may be less than 24 hours times the number of days between them, if there is a daylight saving change in between.

Instead of rounding the computed number of days, you can ignore Daylight Saving Time in `DateTime` calculations by using **UTC** `DateTime` objects<sup>1</sup> because UTC does not observe DST.

Therefore, to compute the difference in days between two dates, ignoring the time (and also ignoring Daylight Saving adjustments and time zones), construct new UTC `DateTime` objects with the same dates and that use the same time of day:

```dart
/// Returns the number of calendar days between [later] and [earlier], ignoring
/// time of day.
///
/// Returns a positive number if [later] occurs after [earlier].
int differenceInCalendarDays(DateTime later, DateTime earlier) {
// Normalize [DateTime] objects to UTC and to discard time information.
later = DateTime.utc(later.year, later.month, later.day);
earlier = DateTime.utc(earlier.year, earlier.month, earlier.day);

return later.difference(earlier).inDays;
}
```

## Update

I've added a [`calendarDaysTill`](

[To see links please register here]

) extension method to [`package:basics`](

[To see links please register here]

) that can do this.

----

<sup>1</sup> Be aware that *converting* a local `DateTime` object to UTC with `.toUtc()` will not help; `dateTime` and `dateTime.toUtc()` both represent the same moment in time, so `dateTime1.difference(dateTime2)` and `dateTime1.toUtc().difference(dateTime.toUtc())` would return the same `Duration`.
Reply

#14
void main() {
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-09-12 10:57:00");

Duration diff = dt1.difference(dt2);

print(diff.inDays);
//output (in days): 1198

print(diff.inHours);

//output (in hours): 28752

}

Reply

#15
Simplest solution:
==================

// d2.difference(d1).inDays

void main() {
final d1 = DateTime.now();
final d2 = d1.add(Duration(days: 2));
print(d2.difference(d1).inDays);
}

Check it out on [DartPad example][1]


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through