Skip to content

DateTime returns wrong value when adding 7 days to the "2021-10-25" date #52223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
IFL-byte opened this issue Apr 30, 2023 · 2 comments
Closed
Labels
closed-as-intended Closed as the reported issue is expected behavior

Comments

@IFL-byte
Copy link

This code prints a wrong value 2021-10-31 instead of the correct value that would be 2021-11-1.

void main() {
DateTime date = DateTime.parse("2021-10-25");
DateTime date2 = date.add(Duration(days: 7));
print(date2);
}

I tried with other similar dates as "2021-01-25" and it works perfectly but not for the one I mentioned above.

@julemand101
Copy link
Contributor

See: #37449

@lrhn
Copy link
Member

lrhn commented Apr 30, 2023

Yes, this is working as intended. Durations are millisecond counts, they do not understand the concept of days, a "day" is just 86400000 milliseconds. Local time-zone DateTime objects have hours, minutes, etc. too, even if they are zero.
And they're subject to local daylight saving changes to time zones.

Use UTC-dates when you want to represent calendar days.

@lrhn lrhn closed this as completed Apr 30, 2023
@lrhn lrhn added the closed-as-intended Closed as the reported issue is expected behavior label Apr 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-as-intended Closed as the reported issue is expected behavior
Projects
None yet
Development

No branches or pull requests

3 participants