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
You can add a duration to a datetime field, e.g. if your fieldname is "DateTime": [DateTime] + #duration (0,10,0,0)) The arguments of#duration are days, hours, minutes, seconds. Specializing in Power Query Formula Language (M)
An object whose value is the sum of the date and time represented by this instance and the time interval represented by value. The resulting DateTime is less than MinValue or greater than MaxValue. The following example demonstrates the Add method. It calculates the day of the week that is 36 days (864 hours) from this moment.
You can use the Add method to add more than one kind of time interval (days, hours, minutes, seconds, or milliseconds) in a single operation. This method's behavior is identical to that of the addition operator. The DateTime structure also supports specialized addition methods (such as AddDays, AddHours, and AddMinutes) for each time interval.
Enter a date and time, then add or subtract any number of months, days, hours, or seconds. Need some help? Duration Between Two Dates – Calculates number of days.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With