flutter DateTime shows that October got 32 days

116 Views Asked by At

I've got a problem with DateTime and I don't know what's the matter. I got a calendar app. When I switch the month forward to October of the current year (or the October of the next year, same issue) the October shows a quantity of 32 days. The other months work properly. I don't know where the problem is. I show some code, but I'm not sure, whether there is a problem in my code or in the DateTime object.

@override
  Widget build(BuildContext context) {
    final userModelData = Provider.of<UserModel>(context);
    final userModel = userModelData.items;
    final dateModelData = Provider.of<DateModel>(context);
    final actualDate = dateModelData.getActualSavedDate!;
    final actualDateTestMonth = dateModelData.getActualSavedDate!.month;
    final dateMonthTest = dateModelData.getMonat;
    final anzahlColumns = userModel.length;

    var dateMonth = DateTime(actualDate.year, actualDate.month, actualDate.day)
        .getDaysInMonth;

    var wochenTag = DateTime(
      actualDate.year,
      actualDate.month,
    );

    List<User> listeColumns() {
      List<User> gesamteColumns = [];
      for (int d = 1; d <= anzahlColumns; d++) {
        gesamteColumns.add(userModel[d - 1]);
      }
      //gesamteColumns += ['gemeinsame Termine', 'Geburtstage'];
      return gesamteColumns;
    }

    List<String> listeRows() {
      List<String> gesamteRows = [];
      for (int d = 1; d <= dateMonth; d++)
        gesamteRows.add(
          actualDate.format('$d'),
        );
      return gesamteRows;
    }
...
1

There are 1 best solutions below

2
Randal Schwartz On

I address how 23-hour and 25-hour days can mess up date calculations in a couple of videos: https://youtu.be/usFSVUEadyo and https://youtu.be/LpoBYgzKVwU.

In brief, adding days: 1 to a DateTime is always adding 24 hours. If we're at the beginning of a 25-hour day, we stay within the same day. If we're just before a 23-hour day, we might miss it entirely and "skip" a day.

Stupid DST. Do all your time calculations in UTC, please.