class
Duration

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.

To create a new Duration object, use this class's single constructor giving the appropriate arguments:

Duration fastestMarathon = new 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, minutes can be greater than 59.

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,

Duration aLongWeekend = new 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.

Implements

Constants

int MICROSECONDS_PER_MILLISECOND = 1000
const
int MILLISECONDS_PER_SECOND = 1000
const
int SECONDS_PER_MINUTE = 60
const
int MINUTES_PER_HOUR = 60
const
int HOURS_PER_DAY = 24
const
int MICROSECONDS_PER_SECOND = MICROSECONDS_PER_MILLISECOND * MILLISECONDS_PER_SECOND
const
int MICROSECONDS_PER_MINUTE = MICROSECONDS_PER_SECOND * SECONDS_PER_MINUTE
const
int MICROSECONDS_PER_HOUR = MICROSECONDS_PER_MINUTE * MINUTES_PER_HOUR
const
int MICROSECONDS_PER_DAY = MICROSECONDS_PER_HOUR * HOURS_PER_DAY
const
int MILLISECONDS_PER_MINUTE = MILLISECONDS_PER_SECOND * SECONDS_PER_MINUTE
const
int MILLISECONDS_PER_HOUR = MILLISECONDS_PER_MINUTE * MINUTES_PER_HOUR
const
int MILLISECONDS_PER_DAY = MILLISECONDS_PER_HOUR * HOURS_PER_DAY
const
int SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
const
int SECONDS_PER_DAY = SECONDS_PER_HOUR * HOURS_PER_DAY
const
int MINUTES_PER_DAY = MINUTES_PER_HOUR * HOURS_PER_DAY
const
Duration ZERO = const Duration(seconds: 0)
const

Properties

int inDays
read-only
Returns the number of whole days spanned by this Duration.
int inHours
read-only
Returns the number of whole hours spanned by this Duration.
int inMinutes
read-only
Returns the number of whole minutes spanned by this Duration.
int inSeconds
read-only
Returns the number of whole seconds spanned by this Duration.
int inMilliseconds
read-only
Returns number of whole milliseconds spanned by this Duration.
int inMicroseconds
read-only
Returns number of whole microseconds spanned by this Duration.
int hashCode
read-only
bool isNegative
read-only
Returns whether this Duration is negative.

Constructors

Duration({int days: 0, int hours: 0, int minutes: 0, int seconds: 0, int milliseconds: 0, int microseconds: 0})
const
Creates a new Duration object whose value is the sum of all individual parts.

Operators

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 *(num factor) → Duration
Multiplies this Duration by the given factor and returns the result as a new Duration object.
operator ~/(int quotient) → Duration
Divides this Duration by the given quotient and returns the truncated result as a new Duration object.
operator <(Duration other) → bool
Returns true if the value of this Duration is less than the value of other.
operator >(Duration other) → bool
Returns true if the value of this Duration is greater than the value of other.
operator <=(Duration other) → bool
Returns true if the value of this Duration is less than or equal to the value of other.
operator >=(Duration other) → bool
Returns true if the value of this Duration is greater than or equal to the value of other.
operator ==(other) → bool
Returns true if this Duration is the same object as other.
operator unary-() → Duration
Returns a new Duration representing this Duration negated.

Methods

compareTo(Duration other) → int
Compares this Duration to other, returning zero if the values are equal.
toString() → String
Returns a string representation of this Duration.
abs() → Duration
Returns a new Duration representing the absolute value of this Duration.