A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds.
A Duration represents a difference from one point in time to another. The duration may be "negative" if the difference is from a later time to an earlier.
Durations are context independent. For example, a duration of 2 days is always 48 hours, even when it is added to a DateTime just when the time zone is about to make a daylight-savings switch. (See DateTime.add).
Despite the same name, a Duration object does not implement "Durations" as specified by ISO 8601. In particular, a duration object does not keep track of the individually provided members (such as "days" or "hours"), but only uses these arguments to compute the length of the corresponding time interval.
To create a new Duration object, use this class's single constructor giving the appropriate arguments:
const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);
The Duration represents a single number of microseconds, which is the sum of all the individual arguments to the constructor.
Properties can access that single number in different ways. For example the inMinutes gives the number of whole minutes in the total duration, which includes the minutes that were provided as "hours" to the constructor, and can be larger than 59.
const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2); print(fastestMarathon.inDays); // 0 print(fastestMarathon.inHours); // 2 print(fastestMarathon.inMinutes); // 123 print(fastestMarathon.inSeconds); // 7382 print(fastestMarathon.inMilliseconds); // 7382000
The duration can be negative, in which case all the properties derived from the duration are also non-positive.
const overDayAgo = Duration(days: -1, hours: -10); print(overDayAgo.inDays); // -1 print(overDayAgo.inHours); // -34 print(overDayAgo.inMinutes); // -2040
Use one of the properties, such as inDays, to retrieve the integer value of the Duration in the specified time unit. Note that the returned value is rounded down. For example,
const aLongWeekend = Duration(hours: 88); print(aLongWeekend.inDays); // 3
This class provides a collection of arithmetic and comparison operators, plus a set of constants useful for converting time units.
const firstHalf = Duration(minutes: 45); // 00:45:00.000000 const secondHalf = Duration(minutes: 45); // 00:45:00.000000 const overTime = Duration(minutes: 30); // 00:30:00.000000 final maxGameTime = firstHalf + secondHalf + overTime; print(maxGameTime.inMinutes); // 120 // The duration of the firstHalf and secondHalf is the same, returns 0. var result = firstHalf.compareTo(secondHalf); print(result); // 0 // Duration of overTime is shorter than firstHalf, returns < 0. result = overTime.compareTo(firstHalf); print(result); // < 0 // Duration of secondHalf is longer than overTime, returns > 0. result = secondHalf.compareTo(overTime); print(result); // > 0
See also:
other, returning zero if the values are equal. factor and returns the result as a new Duration object. other and returns the sum as a new Duration object. other from this Duration and returns the difference as a new Duration object. other. other. other. other. other. quotient and returns the truncated result as a new Duration object. 24 microsecondsPerHour * hoursPerDay microsecondsPerMinute * minutesPerHour 1000 microsecondsPerSecond * secondsPerMinute microsecondsPerMillisecond * millisecondsPerSecond millisecondsPerHour * hoursPerDay millisecondsPerMinute * minutesPerHour millisecondsPerSecond * secondsPerMinute 1000 minutesPerHour * hoursPerDay 60 secondsPerHour * hoursPerDay secondsPerMinute * minutesPerHour 60 Duration(seconds: 0)
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-core/Duration-class.html