Movatterモバイル変換


[0]ホーム

URL:


menu
  1. Dart
  2. dart:core
  3. Duration class
Duration
description

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); // 7235000

The 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); // -2040

Use 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); // 3

This 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); // > 0

See also:

Implemented types

Constructors

Duration({intdays =0,inthours =0,intminutes =0,intseconds =0,intmilliseconds =0,intmicroseconds =0})
Creates a newDuration object whose valueis the sum of all individual parts.
const

Properties

hashCodeint
The hash code for this object.
no setteroverride
inDaysint
The number of entire days spanned by thisDuration.
no setter
inHoursint
The number of entire hours spanned by thisDuration.
no setter
inMicrosecondsint
The number of whole microseconds spanned by thisDuration.
no setter
inMillisecondsint
The number of whole milliseconds spanned by thisDuration.
no setter
inMinutesint
The number of whole minutes spanned by thisDuration.
no setter
inSecondsint
The number of whole seconds spanned by thisDuration.
no setter
isNegativebool
Whether thisDuration is negative.
no setter
runtimeTypeType
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 toother, 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 givenfactor and returns the resultas a new Duration object.
operator +(Durationother)Duration
Adds this Duration andother andreturns the sum as a new Duration object.
operator -(Durationother)Duration
Subtractsother from this Duration andreturns the difference as a new Duration object.
operator<(Durationother)bool
Whether thisDuration is shorter thanother.
operator<=(Durationother)bool
Whether thisDuration is shorter than or equal toother.
operator ==(Objectother)bool
Whether thisDuration has the same length asother.
override
operator >(Durationother)bool
Whether thisDuration is longer thanother.
operator >=(Durationother)bool
Whether thisDuration is longer than or equal toother.
operator unary-()Duration
Creates a newDuration with the opposite direction of thisDuration.
operator ~/(intquotient)Duration
Divides this Duration by the givenquotient and 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.
  1. Dart
  2. dart:core
  3. Duration class
dart:core library

[8]ページ先頭

©2009-2025 Movatter.jp