Reputation: 85
Consider the following code:
void main() {
var duration = new Duration(days : 1);
print ("duration " + duration.toString());
var d1 = new DateTime(2014, 10, 26);
print ("d1 " + d1.toString());
d1 = d1.add(duration);
print ("d1 + duration " + d1.toString());
var d2 = new DateTime(2014, 10, 20);
print ("d2 " + d2.toString());
d2 = d2.add(duration);
print ("d2 + duration " + d2.toString());
}
and the output:
duration 24:00:00.000000
d1 2014-10-26 00:00:00.000
d1 + duration 2014-10-26 23:00:00.000
d2 2014-10-20 00:00:00.000
d2 + duration 2014-10-21 00:00:00.000
Why does October 20 and 26 behave differently. I have checked the same code for every day of the year and every year has one day in which the date + 1 day equals the same date. Every year the date seems to be in October between 25/10 and 30/10.
Is this a bug or have I missed something?
Regards Peyman
Upvotes: 5
Views: 9315
Reputation: 53
Just to expand on Александр Бабич answer - you don't have to care about the range of the days in the month when creating the DateTime in constructor. For example
DateTime(2014, 9, 57)
will correctly return 2014-10-27 and will not introduce any daylight saving shifts. Negative numbers work as well, but are offset by 1, because 0 also works e.g.
DateTime(2014, 9, 0)
DateTime(2014, 9, -1)
will yield 2014-08-31 and 2014-08-30 respectively
Upvotes: 0
Reputation: 1452
As per Günter Zöchbauer answer - it is due daylight saving time.
To properly add day you may use the following:
var d1 = new DateTime(2014, 10, 26);
var d1 = new DateTime(d1.year, d1.month, d1.day + 1);
Upvotes: 8
Reputation: 657476
I guess the Oct 26. (and the other days between 25/10 and 30/10 is due to daylight saving period ending. The difference of 1h (23:00:00.000) indicates this as the cause.
Upvotes: 4