Duration class
A span of time, such as 27 days, 4 hours, 12 minutes, and 3 seconds.
ADuration represents a difference from one point in time to another. Theduration may be "negative" if the difference is from a later time to anearlier.
Durations are context independent. For example, a duration of 2 days isalways 48 hours, even when it is added to aDateTime just when thetime zone is about to make a daylight-savings switch. (SeeDateTime.add).
Despite the same name, aDuration object does not implement "Durations"as specified by ISO 8601. In particular, a duration object does not keeptrack of the individually provided members (such as "days" or "hours"), butonly uses these arguments to compute the length of the corresponding timeinterval.
To create a newDuration object, use this class's single constructorgiving the appropriate arguments:
const fastestMarathon = Duration(hours: 2, minutes: 3, seconds: 2);TheDuration 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 theinMinutes gives the number of whole minutesin the total duration, which includes the minutes that were providedas "hours" to the constructor, and can be larger than 59.
const fastestMarathon = Duration(hours: 2, minutes: 0, seconds: 35);print(fastestMarathon.inDays); // 0print(fastestMarathon.inHours); // 2print(fastestMarathon.inMinutes); // 120print(fastestMarathon.inSeconds); // 7235print(fastestMarathon.inMilliseconds); // 7235000The duration can be negative, in which caseall the properties derived from the duration are also non-positive.
const overDayAgo = Duration(days: -1, hours: -10);print(overDayAgo.inDays); // -1print(overDayAgo.inHours); // -34print(overDayAgo.inMinutes); // -2040Use one of the properties, such asinDays,to retrieve the integer value of theDuration in the specified time unit.Note that the returned value is rounded down.For example,
const aLongWeekend = Duration(hours: 88);print(aLongWeekend.inDays); // 3This class provides a collection of arithmeticand comparison operators,plus a set of constants useful for converting time units.
const firstHalf = Duration(minutes: 45); // 00:45:00.000000const secondHalf = Duration(minutes: 45); // 00:45:00.000000const overTime = Duration(minutes: 30); // 00:30:00.000000final 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); // > 0See also:
- Implemented types
Constructors
Properties
- hashCode→int
- The hash code for this object.no setteroverride
- inDays→int
- The number of entire days spanned by thisDuration.no setter
- inHours→int
- The number of entire hours spanned by thisDuration.no setter
- inMicroseconds→int
- The number of whole microseconds spanned by thisDuration.no setter
- inMilliseconds→int
- The number of whole milliseconds spanned by thisDuration.no setter
- inMinutes→int
- The number of whole minutes spanned by thisDuration.no setter
- inSeconds→int
- The number of whole seconds spanned by thisDuration.no setter
- isNegative→bool
- Whether thisDuration is negative.no setter
- runtimeType→Type
- A representation of the runtime type of the object.no setterinherited
Methods
- abs(
)→Duration - Creates a newDuration representing the absolute length of thisDuration.
- compareTo(
Durationother)→int - Compares thisDuration to
other, returning zero if the values are equal.override - noSuchMethod(
Invocationinvocation)→ dynamic - Invoked when a nonexistent method or property is accessed.inherited
- toString(
)→String - Returns a string representation of thisDuration.override
Operators
- operator *(
numfactor)→Duration - Multiplies this Duration by the given
factorand returns the resultas a new Duration object. - operator +(
Durationother)→Duration - Adds this Duration and
otherandreturns the sum as a new Duration object. - operator -(
Durationother)→Duration - Subtracts
otherfrom this Duration andreturns the difference as a new Duration object. - operator<(
Durationother)→bool - Whether thisDuration is shorter than
other. - operator<=(
Durationother)→bool - Whether thisDuration is shorter than or equal to
other. - operator ==(
Objectother)→bool - Whether thisDuration has the same length as
other.override - operator >(
Durationother)→bool - Whether thisDuration is longer than
other. - operator >=(
Durationother)→bool - Whether thisDuration is longer than or equal to
other. - operator unary-(
)→Duration - Creates a newDuration with the opposite direction of thisDuration.
- operator ~/(
intquotient)→Duration - Divides this Duration by the given
quotientand returns the truncatedresult as a new Duration object.
Constants
- hoursPerDay→ constint
- The number of hours per day.
- microsecondsPerDay→ constint
- The number of microseconds per day.
- microsecondsPerHour→ constint
- The number of microseconds per hour.
- microsecondsPerMillisecond→ constint
- The number of microseconds per millisecond.
- microsecondsPerMinute→ constint
- The number of microseconds per minute.
- microsecondsPerSecond→ constint
- The number of microseconds per second.
- millisecondsPerDay→ constint
- The number of milliseconds per day.
- millisecondsPerHour→ constint
- The number of milliseconds per hour.
- millisecondsPerMinute→ constint
- The number of milliseconds per minute.
- millisecondsPerSecond→ constint
- The number of milliseconds per second.
- minutesPerDay→ constint
- The number of minutes per day.
- minutesPerHour→ constint
- The number of minutes per hour.
- secondsPerDay→ constint
- The number of seconds per day.
- secondsPerHour→ constint
- The number of seconds per hour.
- secondsPerMinute→ constint
- The number of seconds per minute.
- zero→ constDuration
- An empty duration, representing zero time.