Duration class Null safety
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 do 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:
var fastestMarathon = const Duration(hours: 2, minutes: 3, seconds: 2);
The Duration is the sum of all individual parts. This means that individual parts can be larger than the next-bigger unit. For example, inMinutes can be greater than 59.
const fastestMarathon = const Duration(hours: 2, minutes: 3, seconds: 2);
assert(fastestMarathon.inMinutes == 123);
All individual parts are allowed to be negative.
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,
var aLongWeekend = const Duration(hours: 88);
assert(aLongWeekend.inDays == 3);
This class provides a collection of arithmetic and comparison operators, plus a set of constants useful for converting time units.
See DateTime to represent a point in time. See Stopwatch to measure time-spans.
- Implemented types
Constructors
Properties
- hashCode → int
-
The hash code for this object. [...]
read-only, override
- inDays → int
-
The number of entire days spanned by this Duration.
read-only
- inHours → int
-
The number of entire hours spanned by this Duration. [...]
read-only
- inMicroseconds → int
-
The number of whole microseconds spanned by this Duration. [...]
read-only
- inMilliseconds → int
-
The number of whole milliseconds spanned by this Duration. [...]
read-only
- inMinutes → int
-
The number of whole minutes spanned by this Duration. [...]
read-only
- inSeconds → int
-
The number of whole seconds spanned by this Duration. [...]
read-only
- isNegative → bool
-
Whether this Duration is negative. [...]
read-only
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Methods
-
abs(
) → Duration - Creates a new Duration representing the absolute length of this Duration. [...]
-
compareTo(
Duration other) → int -
Compares this Duration to
other
, returning zero if the values are equal. [...]override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed. [...]
inherited
-
toString(
) → String -
Returns a string representation of this Duration. [...]
override
Operators
-
operator *(
num factor) → Duration -
Multiplies this Duration by the given
factor
and returns the result as a new Duration object. [...] -
operator +(
Duration other) → Duration -
Adds this Duration and
other
and returns the sum as a new Duration object. -
operator -(
Duration other) → Duration -
Subtracts
other
from this Duration and returns the difference as a new Duration object. -
operator <(
Duration other) → bool -
Whether this Duration is shorter than
other
. -
operator <=(
Duration other) → bool -
Whether this Duration is shorter than or equal to
other
. -
operator ==(
Object other) → bool -
Whether this Duration has the same length as
other
. [...]override -
operator >(
Duration other) → bool -
Whether this Duration is longer than
other
. -
operator >=(
Duration other) → bool -
Whether this Duration is longer than or equal to
other
. -
operator unary-(
) → Duration - Creates a new Duration with the opposite direction of this Duration. [...]
-
operator ~/(
int quotient) → Duration -
Divides this Duration by the given
quotient
and returns the truncated result as a new Duration object. [...]
Constants
- hoursPerDay → const int
-
The number of hours per day. [...]
24
- microsecondsPerDay → const int
-
The number of microseconds per day.
microsecondsPerHour * hoursPerDay
- microsecondsPerHour → const int
-
The number of microseconds per hour.
microsecondsPerMinute * minutesPerHour
- microsecondsPerMillisecond → const int
-
The number of microseconds per millisecond.
1000
- microsecondsPerMinute → const int
-
The number of microseconds per minute.
microsecondsPerSecond * secondsPerMinute
- microsecondsPerSecond → const int
-
The number of microseconds per second.
microsecondsPerMillisecond * millisecondsPerSecond
- millisecondsPerDay → const int
-
The number of milliseconds per day.
millisecondsPerHour * hoursPerDay
- millisecondsPerHour → const int
-
The number of milliseconds per hour.
millisecondsPerMinute * minutesPerHour
- millisecondsPerMinute → const int
-
The number of milliseconds per minute.
millisecondsPerSecond * secondsPerMinute
- millisecondsPerSecond → const int
-
The number of milliseconds per second.
1000
- minutesPerDay → const int
-
The number of minutes per day.
minutesPerHour * hoursPerDay
- minutesPerHour → const int
-
The number of minutes per hour.
60
- secondsPerDay → const int
-
The number of seconds per day.
secondsPerHour * hoursPerDay
- secondsPerHour → const int
-
The number of seconds per hour.
secondsPerMinute * minutesPerHour
- secondsPerMinute → const int
-
The number of seconds per minute. [...]
60
- zero → const Duration
-
An empty duration, representing zero time.
Duration(seconds: 0)