Skip to content

DateTime.add(Duration days) produces wrong date #37449

Closed
@natgross

Description

@natgross

/*
Bug description: Adding duration of Days to DateTime is consistently WRONG in certain months.
DateTime mysteriously adds 23 hours (in month 11, Nov) in addition to the days
resulting in WRONG date and of course day of week. In a loop it continues with this error
until mysteriously correcting itself a few months later.
In the following test, I'm adding 7 days in the loop and also printing the weekday so that you can
spot the problem easily. Also of course keep an eye on the hours in the dates printed.
Printing several years to prove bug consistency every year, at same month.
[Update: Possibly related to switching back from DST]
*/

void main() {
  int numOfLoops = 40;
  for (int year = 2015; year < 2022; year++) {
    DateTime dateTime = DateTime(year, 10, 1);
    for (int i= 0; i<numOfLoops;i++ ) {
      print ('Starting Year:$year Loop# $i');
      print ('BEFORE ADDING 7 DAYS: $dateTime. \t weekday = ${dateTime.weekday}' );
      dateTime = dateTime.add(Duration(days:7));
      print (' AFTER ADDING 7 DAYS: $dateTime. \t weekday = ${dateTime.weekday}' );
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    closed-as-intendedClosed as the reported issue is expected behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions