Duration represents a discrete, immutable span of time, independent of calendar systems, time zones, or specific clock times. Internally, it encapsulates the time difference as a signed 64-bit integer representing the total number of microseconds.
Instantiation and Normalization
TheDuration constructor accepts named arguments for various time units ranging from days down to microseconds. All arguments are optional and default to 0.
The constructor normalizes input values. If a unit exceeds its natural boundary (e.g., 65 seconds), the excess is carried over to the next larger unit. Negative values are permitted and are subtracted from the total span.
Properties and Accessors
TheDuration class provides getters to retrieve the span in specific units. These accessors return the total span expressed in the requested unit, truncated to an integer. They do not return the fractional component of the time format.
inDays: Total days.inHours: Total hours.inMinutes: Total minutes.inSeconds: Total seconds.inMilliseconds: Total milliseconds.inMicroseconds: Total microseconds (the exact internal representation).
isNegative: Returnstrueif the total microseconds is less than 0.
Arithmetic and Comparison
Duration objects support operator overloading for arithmetic manipulation and comparison. Because the class is immutable, arithmetic operations return a new Duration instance.
Arithmetic Operators:
+(Addition): Adds two durations.-(Subtraction): Subtracts one duration from another.*(Multiplication): Multiplies the duration by a number.~/(Integer Division): Divides the duration by a number, returning a truncatedDuration.-(Unary Negation): Negates the duration.
Duration implements Comparable<Duration>, allowing the use of standard comparison operators (>, <, >=, <=, ==).
Constants
The class exposes static constants for common time calculations:Duration.zero: An empty duration (0 microseconds).Duration.microsecondsPerMillisecond: 1000Duration.millisecondsPerSecond: 1000Duration.secondsPerMinute: 60Duration.minutesPerHour: 60Duration.hoursPerDay: 24
Master Dart with Deep Grasping Methodology!Learn More





