← All languages
Dart function of the day
Random

Duration

Represent a span of time with various unit constructors.

Description

The Duration class represents a fixed length of time. It can be created using named parameters for days, hours, minutes, seconds, milliseconds, and microseconds. Multiple parameters can be combined, and the total duration is the sum of all specified units.

Duration objects support arithmetic operations including addition, subtraction, multiplication, and division. They also support comparison operators and can be used with DateTime for date arithmetic. The inDays, inHours, inMinutes, inSeconds, and inMilliseconds getters provide the total duration in those units.

Duration is commonly used with Future.delayed for timers, with Stopwatch for measuring elapsed time, and with animation controllers in Flutter for specifying animation durations.

Arguments

NameDescriptionOptional
days The number of days. Defaults to 0. Yes
hours The number of hours. Defaults to 0. Yes
minutes The number of minutes. Defaults to 0. Yes
seconds The number of seconds. Defaults to 0. Yes
milliseconds The number of milliseconds. Defaults to 0. Yes

Example

var d = Duration(hours: 1, minutes: 30);
print(d.inMinutes); // 90
print(d.inSeconds); // 5400

var future = DateTime.now().add(Duration(days: 7));
print(future); // one week from now

Reference