2

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;
    }
...
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
Pauer Auer
  • 101
  • 10
  • which year is that? – eeqk Jul 10 '22 at 12:23
  • 1
    I am guessing it's to do with the extra hour some countries in the northern hemisphere have in October. You'll probably find the same with the southern hemisphere in March. Welcome to timezone hell... – Bib Jul 10 '22 at 12:24
  • 1
    When you just want to do calendar maths, and don't care about timezones, use the `DateTime.utc()` constructor. – Richard Heap Jul 10 '22 at 15:11

1 Answers1

2

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.

Randal Schwartz
  • 39,428
  • 4
  • 43
  • 70
  • but how do I get the correct days in a month (especially in October)? The method getdaysinmonth is part of the dart_date package, so I don't calculate the days in a month manually. By the way, I tried var "dateMonth= DateTime.utc(actualDate.year, actualDate.month).getDaysInMonth" but this does't work as well – Pauer Auer Jul 16 '22 at 09:45
  • `final now = DateTime.now(); final lastDayOfThisLocalMonth = DateTime.utc(now.year,now.month+1,-1); final days = lastDayOfThisLocalMonth.day;` Yes, adding 1 month, and going to the -1 day works to get to the last day of the current month. :) – Randal Schwartz Jul 16 '22 at 23:50