Noda Time
Show / Hide Table of Contents

Struct Duration

Represents a fixed (and calendar-independent) length of time.
Since 1.0.x
Availability net35-Client, PCL
Implements
IEquatable<Duration>
IComparable<Duration>
IComparable
Inherited Members
Object.Equals(Object, Object)
Object.ReferenceEquals(Object, Object)
Object.GetType()
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
public struct Duration : IEquatable<Duration>, IComparable<Duration>, IComparable
Remarks

A duration is a length of time defined by an integral number of 'ticks', where a tick is equal to 100 nanoseconds. There are 10,000 ticks in a millisecond. Although durations are usually used with a positive number of ticks, negative durations are valid, and may occur naturally when e.g. subtracting a later Instant from an earlier one.

A duration represents a fixed length of elapsed time along the time line that occupies the same amount of time regardless of when it is applied. In contrast, Period represents a period of time in calendrical terms (hours, days, and so on) that may vary in elapsed time when applied.

In general, use Duration to represent durations applied to global types like Instant and ZonedDateTime; use Period to represent a period applied to local types like LocalDateTime.

Fields

Epsilon

Represents the Duration value equals to 1 tick; the smallest amount by which an instant can vary. This field is read-only.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static readonly Duration Epsilon
Field Value
Type Description
Duration

Zero

Represents the zero Duration value. This field is read-only.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static readonly Duration Zero
Field Value
Type Description
Duration

Properties

Ticks

The total number of ticks in the duration.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public long Ticks { get; }
Property Value
Type Description
Int64
Remarks
This property effectively represents all of the information within a Duration value; a duration is simply a number of ticks.

Methods

Add(Duration, Duration)

Adds one duration to another. Friendly alternative to operator+().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Add(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the sum of the given values.

CompareTo(Duration)

Compares the current object with another object of the same type.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public int CompareTo(Duration other)
Parameters
Type Name Description
Duration other An object to compare with this object.
Returns
Type Description
Int32 A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has the following meanings:
ValueMeaning
< 0This object is less than the other parameter.
0This object is equal to other.
> 0This object is greater than other.

Divide(Duration, Int64)

Divides a duration by a number. Friendly alternative to operator/().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Divide(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Int64 right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of dividing left by right.

Equals(Duration)

Indicates whether the current object is equal to another object of the same type.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public bool Equals(Duration other)
Parameters
Type Name Description
Duration other An object to compare with this object.
Returns
Type Description
Boolean true if the current object is equal to the other parameter; otherwise, false.

Equals(Object)

Determines whether the specified System.Object is equal to this instance.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public override bool Equals(object obj)
Parameters
Type Name Description
Object obj The System.Object to compare with this instance.
Returns
Type Description
Boolean true if the specified System.Object is equal to this instance; otherwise, false.
Overrides
System.ValueType.Equals(System.Object)

FromHours(Int64)

Returns a Duration that represents the given number of hours.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromHours(long hours)
Parameters
Type Name Description
Int64 hours The number of hours.
Returns
Type Description
Duration A Duration representing the given number of hours.

FromMilliseconds(Int64)

Returns a Duration that represents the given number of milliseconds.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromMilliseconds(long milliseconds)
Parameters
Type Name Description
Int64 milliseconds The number of milliseconds.
Returns
Type Description
Duration A Duration representing the given number of milliseconds.

FromMinutes(Int64)

Returns a Duration that represents the given number of minutes.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromMinutes(long minutes)
Parameters
Type Name Description
Int64 minutes The number of minutes.
Returns
Type Description
Duration A Duration representing the given number of minutes.

FromSeconds(Int64)

Returns a Duration that represents the given number of seconds.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromSeconds(long seconds)
Parameters
Type Name Description
Int64 seconds The number of seconds.
Returns
Type Description
Duration A Duration representing the given number of seconds.

FromStandardDays(Int64)

Returns a Duration that represents the given number of days, assuming a 'standard' 24-hour day.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromStandardDays(long days)
Parameters
Type Name Description
Int64 days The number of days.
Returns
Type Description
Duration A Duration representing the given number of days.

FromStandardWeeks(Int64)

Returns a Duration that represents the given number of weeks, assuming a 'standard' week consisting of seven 24-hour days.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromStandardWeeks(long weeks)
Parameters
Type Name Description
Int64 weeks The number of weeks.
Returns
Type Description
Duration A Duration representing the given number of weeks.

FromTicks(Int64)

Returns a Duration that represents the given number of ticks.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromTicks(long ticks)
Parameters
Type Name Description
Int64 ticks The number of ticks.
Returns
Type Description
Duration A Duration representing the given number of ticks.

FromTimeSpan(TimeSpan)

Returns a Duration that represents the same number of ticks as the given System.TimeSpan.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration FromTimeSpan(TimeSpan timeSpan)
Parameters
Type Name Description
TimeSpan timeSpan The TimeSpan value to convert
Returns
Type Description
Duration A new Duration with the same number of ticks as the given TimeSpan.

GetHashCode()

Returns a hash code for this instance.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public override int GetHashCode()
Returns
Type Description
Int32 A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
Overrides
System.ValueType.GetHashCode()

Minus(Duration)

Returns the result of subtracting another duration from this one, for a fluent alternative to operator-().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public Duration Minus(Duration other)
Parameters
Type Name Description
Duration other The duration to subtract
Returns
Type Description
Duration A new Duration representing the result of the subtraction.

Multiply(Duration, Int64)

Multiplies a duration by a number. Friendly alternative to operator*().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Multiply(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Int64 right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the product of the given values.

Multiply(Int64, Duration)

Multiplies a duration by a number. Friendly alternative to operator*().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Multiply(long left, Duration right)
Parameters
Type Name Description
Int64 left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the product of the given values.

Negate(Duration)

Implements a friendly alternative to the unary negation operator.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Negate(Duration duration)
Parameters
Type Name Description
Duration duration Duration to negate
Returns
Type Description
Duration The negative value of this duration

Plus(Duration)

Returns the result of adding another duration to this one, for a fluent alternative to operator+().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public Duration Plus(Duration other)
Parameters
Type Name Description
Duration other The duration to add
Returns
Type Description
Duration A new Duration representing the result of the addition.

Subtract(Duration, Duration)

Subtracts one duration from another. Friendly alternative to operator-().
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration Subtract(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the difference of the given values.

ToString()

Gets the value as a System.String including the number of ticks represented by this duration.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public override string ToString()
Returns
Type Description
String A string representation of this duration.
Overrides
System.ValueType.ToString()

ToTimeSpan()

Returns a System.TimeSpan that represents the same number of ticks as this Duration.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public TimeSpan ToTimeSpan()
Returns
Type Description
TimeSpan A new TimeSpan with the same number of ticks as this Duration.

Operators

Addition(Duration, Duration)

Implements the operator + (addition).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator +(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the sum of the given values.

Division(Duration, Int64)

Implements the operator / (division).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator /(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Int64 right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of dividing left by right.

Equality(Duration, Duration)

Implements the operator == (equality).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator ==(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if values are equal to each other, otherwise false.

GreaterThan(Duration, Duration)

Implements the operator > (greater than).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator>(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if the left value is greater than the right value, otherwise false.

GreaterThanOrEqual(Duration, Duration)

Implements the operator >= (greater than or equal).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator >=(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if the left value is greater than or equal to the right value, otherwise false.

Inequality(Duration, Duration)

Implements the operator != (inequality).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator !=(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if values are not equal to each other, otherwise false.

LessThan(Duration, Duration)

Implements the operator < (less than).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator <(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if the left value is less than the right value, otherwise false.

LessThanOrEqual(Duration, Duration)

Implements the operator <= (less than or equal).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static bool operator <=(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Boolean true if the left value is less than or equal to the right value, otherwise false.

Multiply(Duration, Int64)

Implements the operator * (multiplication).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator *(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Int64 right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of multiplying left by right.

Multiply(Int64, Duration)

Implements the operator * (multiplication).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator *(long left, Duration right)
Parameters
Type Name Description
Int64 left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of multiplying left by right.

Subtraction(Duration, Duration)

Implements the operator - (subtraction).
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator -(Duration left, Duration right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
Duration right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the difference of the given values.

UnaryNegation(Duration)

Implements the unary negation operator.
Since 1.0.x
Availability net35-Client, PCL
Declaration
public static Duration operator -(Duration duration)
Parameters
Type Name Description
Duration duration Duration to negate
Returns
Type Description
Duration The negative value of this duration

Explicit Interface Implementations

IComparable.CompareTo(Object)

Implementation of System.IComparable.CompareTo(System.Object) to compare two offsets.
Since 1.0.x
Availability net35-Client, PCL
Declaration
int IComparable.CompareTo(object obj)
Parameters
Type Name Description
Object obj The object to compare this value with.
Returns
Type Description
Int32 The result of comparing this instant with another one; see CompareTo(Duration) for general details. If obj is null, this method returns a value greater than 0.
Remarks
This uses explicit interface implementation to avoid it being called accidentally. The generic implementation should usually be preferred.
Exceptions
Type Condition
ArgumentException obj is non-null but does not refer to an instance of Duration.

Implements

System.IEquatable<T>
System.IComparable<T>
System.IComparable
In this article
Back to top Generated by DocFX