-
Notifications
You must be signed in to change notification settings - Fork 1.7k
DateTime addDays #53729
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
Comments
I'm not sure if I fully understand what you're looking for, but does the void main() {
final beforeDaylightSavings = DateTime(2023, 11, 4, 12);
final sameTimeNextDay = beforeDaylightSavings.copyWith(
day: beforeDaylightSavings.day + 1,
);
final oneDayLater = beforeDaylightSavings.add(const Duration(days: 1));
print('Timezone: ${beforeDaylightSavings.timeZoneName}');
print('Day before daylight savings: $beforeDaylightSavings');
print('Next day, same time: $sameTimeNextDay');
print('One day (24 hours) later: $oneDayLater');
} Results in the following output: Timezone: Central Daylight Time
Day before daylight savings: 2023-11-04 12:00:00.000
Next day, same time: 2023-11-05 12:00:00.000
One day (24 hours) later: 2023-11-05 11:00:00.000 |
That is exactly what I am looking for @parlough! Could it be an idea to put a reference to that method in the
|
It would also avoid articles like this
And issues like this |
Glad I could point you there! However, do note that (I think) Since
I haven't really worked with |
It's an annoying issue that DateTime add([Duration durationMilliseconds], {int years = 0, int months = 0, int days = 0, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0}); That won't work, partly because it's breaking to change the API because there are classes out there implementing it (they should not), and partly because you can't mix positional and named optional parameters (yet, as I've said for 12 years). So, the good name is taken. And deciding what it means to add +1 day and -25 hours. The trivial approach is probably the best (same as
|
Thank you for opening the discussion again @parlough.
I don't think I understand this limitation properly. Could you give an example of it? Even if the daylight saving occurs it seems to return the DateTime I want. void main() {
final beforeDaylightSaving = DateTime(2023, 10, 29, 02, 30);
print(beforeDaylightSaving);
print(beforeDaylightSaving.timeZoneName);
final afterDaylightSaving = beforeDaylightSaving.copyWith(
minute: beforeDaylightSaving.minute + 30,
);
print(afterDaylightSaving);
print(afterDaylightSaving.timeZoneName);
final withAdd = beforeDaylightSaving.add(Duration(minutes: 30));
print(withAdd);
print(withAdd.timeZoneName);
} 2023-10-29 02:30:00.000
GMT+02:00
2023-10-29 03:00:00.000 // This is what I want
GMT+01:00
2023-10-29 02:00:00.000
GMT+01:00 To rephrase the question: I want |
If you don't want to worry about daylight saving, use UTC. Nothing else will suffice. On the other hand, if you do use UTC, almost any approach will work. |
So, it seems to me the best fix for this is to do |
This is a feature request for a method that can add whole days without being affected by daylight savings.
The
DateTime add(Duration duration)
method adds seconds. Several issues have been created where developers are confused about the behaviour of the method #37449.The
DateTime
API is well documented, but the question on how to add days without being affected by daylight savings is not well documented. This is also causing issues in third party packages like jiffy jama5262/jiffy#81Could we either
DateTime newDate = DateTime(date.year, date.month, date.day + days);
(I don't even know if this is the correct way to do it, some StackOverflow posts mentions that solution)DateTime addDays(int days)
which is not affected by daylight savingsThe text was updated successfully, but these errors were encountered: