Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime and duration add

Tags:

dart

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

like image 415
Peyman Zehtab-Fard Avatar asked Oct 21 '14 12:10

Peyman Zehtab-Fard


People also ask

How do I add a duration to a datetime field?

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)

What is add method in datetime?

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.

How to add more than one time interval in datetime?

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.

How do I calculate the duration of a date?

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.


2 Answers

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);
like image 50
Александр Бабич Avatar answered Oct 19 '22 06:10

Александр Бабич


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.

like image 22
Günter Zöchbauer Avatar answered Oct 19 '22 05:10

Günter Zöchbauer