Issue
In my App, I made a for loop that prints a succession of 600 consecutive dates to match a date entered by the user. I used the INTL library for Dates. here is a part of the code:
var date = []; // is the list that will contain all dates.
unformatted_date = Datetime.now(); //or the one entered by the user
for(var i = 1; i<600; i++){
date.add(unformatted_date.add(Duration(days: i)));
}
If for example, I insert as initial date May 5, 2021, the cycle always prints me twice October 30. Even on other Dates, it happens to have two equal. It rarely happens that the Date is not there, that it is not printed
Solution
The problem is very likely to be DST (Daylight saving time). It is important to understand that add()
on DateTime
is documented with:
Notice that the duration being added is actually 50 * 24 * 60 * 60 seconds. If the resulting
DateTime
has a different daylight saving offset thanthis
, then the result won't have the same time-of-day asthis
, and may not even hit the calendar date 50 days later.Be careful when working with dates in local time.
https://api.dart.dev/stable/2.15.1/dart-core/DateTime/add.html
So if you add a day, it assumes the day is 24 hours which is not true in case of DST shifts.
You should instead use UTC time (since UTC does not have DST) when doing addition on DateTime
and then convert it back to localtime when you need to display the date if the time is important to show correctly to the user.
Answered By - julemand101
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.