Noda Time
Show / Hide Table of Contents

Struct Duration

Represents a fixed (and calendar-independent) length of time.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Implements
IEquatable<Duration>
IComparable<Duration>
IComparable
IXmlSerializable
IFormattable
IAdditionOperators<Duration, Duration, Duration>
ISubtractionOperators<Duration, Duration, Duration>
IUnaryNegationOperators<Duration, Duration>
IUnaryPlusOperators<Duration, Duration>
IComparisonOperators<Duration, Duration, bool>
IEqualityOperators<Duration, Duration, bool>
IMinMaxValue<Duration>
IAdditiveIdentity<Duration, Duration>
Inherited Members
object.Equals(object, object)
object.GetType()
object.ReferenceEquals(object, object)
Namespace: NodaTime
Assembly: NodaTime.dll
Syntax
[TypeConverter(typeof(DurationTypeConverter))]
public readonly struct Duration : IEquatable<Duration>, IComparable<Duration>, IComparable, IXmlSerializable, IFormattable, IAdditionOperators<Duration, Duration, Duration>, ISubtractionOperators<Duration, Duration, Duration>, IUnaryNegationOperators<Duration, Duration>, IUnaryPlusOperators<Duration, Duration>, IComparisonOperators<Duration, Duration, bool>, IEqualityOperators<Duration, Duration, bool>, IMinMaxValue<Duration>, IAdditiveIdentity<Duration, Duration>
Remarks

A duration is a length of time defined by an integral number of nanoseconds. Although durations are usually used with a positive number of nanoseconds, negative durations are valid, and may occur naturally when e.g. subtracting a later Instant from an earlier one.

Equality and ordering are defined in the natural way, simply comparing the number of nanoseconds contained.

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 (years, months, 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.

The range of valid values of a Duration is greater than the span of time supported by Noda Time - so for example, subtracting one Instant from another will always give a valid Duration. The range is also greater than that of TimeSpan (MinValue ticks to MaxValue ticks). See the user guide for more details of the exact range, but it is not expected that this will ever be exceeded in normal code.

Various operations accept or return a double, in-keeping with durations often being natural lengths of time which are imprecisely measured anyway. The implementation of these operations should never result in a not-a-number or infinite value, nor do any operations accept not-a-number or infinite values. Additionally, operations involving Double have initially been implemented fairly naively; it's possible that future releases will improve the accuracy or performance (or both) of various operations.

The default value of this type is Zero.

Properties

AdditiveIdentity

Gets the additive identity.
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration AdditiveIdentity { get; }
Property Value
Type Description
Duration

BclCompatibleTicks

Gets the total number of ticks in the duration as a 64-bit integer, truncating towards zero where necessary.
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long BclCompatibleTicks { get; }
Property Value
Type Description
long The total number of ticks in the duration.
Remarks

Within the constraints specified below, this property is intended to be equivalent to Ticks.

If the number of nanoseconds in a duration is not a whole number of ticks, it is truncated towards zero. For example, durations in the range [-99ns, 99ns] would all count as 0 ticks.

Although this method can overflow, it will only do so in very exceptional cases, with durations with a magnitude of more than 29000 Gregorian years or so.

Exceptions
Type Condition
OverflowException The number of ticks cannot be represented a signed 64-bit integer.
See Also
TotalTicks

Days

Gets the whole number of standard (24 hour) days within this duration. This is truncated towards zero; for example, "-1.75 days" and "1.75 days" would have results of -1 and 1 respectively.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Days { get; }
Property Value
Type Description
int The whole number of days in the duration

Epsilon

Gets a Duration value equal to 1 nanosecond; the smallest amount by which an instant can vary.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Epsilon { get; }
Property Value
Type Description
Duration A duration representing 1 nanosecond.

Hours

The hour component of this duration, in the range [-23, 23], truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Hours { get; }
Property Value
Type Description
int The hour component of the duration, within the day.

MaxValue

Gets the maximum value supported by Duration.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration MaxValue { get; }
Property Value
Type Description
Duration

Milliseconds

Gets the subsecond component of this duration, expressed in milliseconds, in the range [-999, 999] and truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Milliseconds { get; }
Property Value
Type Description
int The subsecond component of the duration, in milliseconds.

MinValue

Gets the minimum (largest negative) value supported by Duration.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration MinValue { get; }
Property Value
Type Description
Duration

Minutes

The minute component of this duration, in the range [-59, 59], truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Minutes { get; }
Property Value
Type Description
int The minute component of the duration, within the hour.

NanosecondOfDay

Gets the number of nanoseconds within the day of this duration. For negative durations, this will be negative (or zero).
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long NanosecondOfDay { get; }
Property Value
Type Description
long The number of nanoseconds within the day of this duration.

Seconds

Gets the second component of this duration, in the range [-59, 59], truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int Seconds { get; }
Property Value
Type Description
int The second component of the duration, within the minute.

SubsecondNanoseconds

Gets the subsecond component of this duration, expressed in nanoseconds, in the range [-999999999, 999999999].
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int SubsecondNanoseconds { get; }
Property Value
Type Description
int The subsecond component of the duration, in nanoseconds.

SubsecondTicks

Gets the subsecond component of this duration, expressed in ticks, in the range [-9999999, 9999999] and truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int SubsecondTicks { get; }
Property Value
Type Description
int The subsecond component of the duration, in ticks.

TotalDays

Gets the total number of days in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalDays { get; }
Property Value
Type Description
double The total number of days in this duration.
Remarks
This property is the Duration equivalent of TotalDays. It represents the complete duration in days, rather than only the whole number of days. For example, for a duration of 36 hours, this property would return 1.5.

TotalHours

Gets the total number of hours in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalHours { get; }
Property Value
Type Description
double The total number of hours in this duration.
Remarks
This property is the Duration equivalent of TotalHours. Unlike Hours, it represents the complete duration in hours rather than the whole number of hours as part of the day. So for a duration of 1 day, 2 hours and 30 minutes, the Hours property will return 2, but TotalHours will return 26.5.

TotalMilliseconds

Gets the total number of milliseconds in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalMilliseconds { get; }
Property Value
Type Description
double The total number of milliseconds in this duration.
Remarks
This property is the Duration equivalent of TotalMilliseconds. Unlike Milliseconds, it represents the complete duration in milliseconds rather than the whole number of milliseconds within the second. So for a duration of 10 minutes, 20 seconds and 250 milliseconds, the Milliseconds property will return 250, but TotalMilliseconds will return 620250.

TotalMinutes

Gets the total number of minutes in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalMinutes { get; }
Property Value
Type Description
double The total number of minutes in this duration.
Remarks
This property is the Duration equivalent of TotalMinutes. Unlike Minutes, it represents the complete duration in minutes rather than the whole number of minutes within the hour. So for a duration of 2 hours, 30 minutes and 45 seconds, the Minutes property will return 30, but TotalMinutes will return 150.75.

TotalNanoseconds

Gets the total number of nanoseconds in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalNanoseconds { get; }
Property Value
Type Description
double This duration as a number of nanoseconds, represented as a Double.
Remarks
The result is always an integer, but may not be precise due to the limitations of the Double type. In other works, Duration.FromNanoseconds(duration.TotalNanoseconds) is not guaranteed to round-trip. To guarantee precision and round-tripping, use ToBigIntegerNanoseconds() and FromNanoseconds(BigInteger) (or the Int128 equivalents where available).

TotalSeconds

Gets the total number of seconds in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalSeconds { get; }
Property Value
Type Description
double The total number of seconds in this duration.
Remarks
This property is the Duration equivalent of TotalSeconds. Unlike Seconds, it represents the complete duration in seconds rather than the whole number of seconds within the minute. So for a duration of 10 minutes, 20 seconds and 250 milliseconds, the Seconds property will return 20, but TotalSeconds will return 620.25.

TotalTicks

Gets the total number of ticks in this duration, as a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public double TotalTicks { get; }
Property Value
Type Description
double The total number of ticks in this duration.
Remarks
This property is the Duration equivalent of Ticks, except represented as double-precision floating point number instead of a 64-bit integer. This is because Duration has a precision of nanoseconds, and also because the range of 64-bit integers doesn't cover the number of possible ticks in a Duration. (The latter is only an issue in durations outside the range of TimeSpan - in other words, with magnitudes of over 29,000 years.)
See Also
BclCompatibleTicks

Zero

Gets a zero Duration of 0 nanoseconds.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Zero { get; }
Property Value
Type Description
Duration The zero Duration value.

Methods

Add(Duration, Duration)

Adds one duration to another. Friendly alternative to operator+().
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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.

AddSchema(XmlSchemaSet)

Adds the XML schema type describing the structure of the Duration XML serialization to the given xmlSchemaSet.
Since 3.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static XmlQualifiedName AddSchema(XmlSchemaSet xmlSchemaSet)
Parameters
Type Name Description
XmlSchemaSet xmlSchemaSet The XML schema set provided by XmlSchemaExporter.
Returns
Type Description
XmlQualifiedName The qualified name of the schema type that was added to the xmlSchemaSet.

CompareTo(Duration)

Implementation of CompareTo(T) to compare two durations. See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public int CompareTo(Duration other)
Parameters
Type Name Description
Duration other An object to compare with this object.
Returns
Type Description
int 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, Duration)

Divides one duration by another. Friendly alternative to operator/().
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static double Divide(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
double The double representing the result of dividing left by right.

Divide(Duration, double)

Divides a duration by a double. Friendly alternative to operator/().
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Divide(Duration left, double right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
double right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of dividing left by right.

Divide(Duration, long)

Divides a duration by an long. Friendly alternative to operator/().
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Divide(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
long 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. See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public bool Equals(Duration other)
Parameters
Type Name Description
Duration other An object to compare with this object.
Returns
Type Description
bool true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Determines whether the specified object is equal to this instance. See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override bool Equals(object? obj)
Parameters
Type Name Description
object obj The object to compare with this instance.
Returns
Type Description
bool true if the specified object is equal to this instance; otherwise, false.
Overrides
ValueType.Equals(object)

FromDays(double)

Returns a Duration that represents the given number of days, assuming a 'standard' 24-hour day.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromDays(double days)
Parameters
Type Name Description
double days The number of days.
Returns
Type Description
Duration A Duration representing the given number of days.

FromDays(int)

Returns a Duration that represents the given number of days, assuming a 'standard' 24-hour day.
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromDays(int days)
Parameters
Type Name Description
int days The number of days.
Returns
Type Description
Duration A Duration representing the given number of days.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromDays(5);
Console.WriteLine(duration.Days);
Console.WriteLine(duration.ToString());

Output:

5
5:00:00:00

FromHours(double)

Returns a Duration that represents the given number of hours.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromHours(double hours)
Parameters
Type Name Description
double hours The number of hours.
Returns
Type Description
Duration A Duration representing the given number of hours.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromHours(1.5);
Console.WriteLine(duration.Hours);
Console.WriteLine(duration.TotalHours);
Console.WriteLine(duration.TotalMinutes);
Console.WriteLine(duration.ToString());

Output:

1
1.5
90
0:01:30:00

FromHours(int)

Returns a Duration that represents the given number of hours.
Since 1.4.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromHours(int hours)
Parameters
Type Name Description
int hours The number of hours.
Returns
Type Description
Duration A Duration representing the given number of hours.

FromMilliseconds(double)

Returns a Duration that represents the given number of milliseconds.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromMilliseconds(double milliseconds)
Parameters
Type Name Description
double milliseconds The number of milliseconds.
Returns
Type Description
Duration A Duration representing the given number of milliseconds.

FromMilliseconds(long)

Returns a Duration that represents the given number of milliseconds.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromMilliseconds(long milliseconds)
Parameters
Type Name Description
long milliseconds The number of milliseconds.
Returns
Type Description
Duration A Duration representing the given number of milliseconds.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromMilliseconds(600);
Console.WriteLine(duration.Milliseconds);
Console.WriteLine(duration.TotalSeconds);
Console.WriteLine(duration.ToString());

Output:

600
0.6
0:00:00:00.6

FromMinutes(double)

Returns a Duration that represents the given number of minutes.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromMinutes(double minutes)
Parameters
Type Name Description
double minutes The number of minutes.
Returns
Type Description
Duration A Duration representing the given number of minutes.

FromMinutes(long)

Returns a Duration that represents the given number of minutes.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromMinutes(long minutes)
Parameters
Type Name Description
long minutes The number of minutes.
Returns
Type Description
Duration A Duration representing the given number of minutes.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromMinutes(50);
Console.WriteLine(duration.Minutes);
Console.WriteLine(duration.ToString());

Output:

50
0:00:50:00

FromNanoseconds(double)

Converts a number of nanoseconds expressed as a double into a duration. Any fractional parts of the value are truncated towards zero.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromNanoseconds(double nanoseconds)
Parameters
Type Name Description
double nanoseconds The number of nanoseconds to represent.
Returns
Type Description
Duration A duration with the given number of nanoseconds.

FromNanoseconds(Int128)

Converts a number of nanoseconds expressed as an Int128 into a duration.
Since 3.2.x
Availability net8.0
Declaration
public static Duration FromNanoseconds(Int128 nanoseconds)
Parameters
Type Name Description
Int128 nanoseconds The number of nanoseconds to represent.
Returns
Type Description
Duration A duration with the given number of nanoseconds.

FromNanoseconds(long)

Returns a Duration that represents the given number of nanoseconds.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromNanoseconds(long nanoseconds)
Parameters
Type Name Description
long nanoseconds The number of nanoseconds.
Returns
Type Description
Duration A Duration representing the given number of nanoseconds.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromNanoseconds(1_000_000_000);
Console.WriteLine(duration.TotalNanoseconds);
Console.WriteLine(duration.TotalSeconds);
Console.WriteLine(duration.ToString());

Output:

1000000000
1
0:00:00:01

FromNanoseconds(BigInteger)

Converts a number of nanoseconds expressed as a BigInteger into a duration.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromNanoseconds(BigInteger nanoseconds)
Parameters
Type Name Description
BigInteger nanoseconds The number of nanoseconds to represent.
Returns
Type Description
Duration A duration with the given number of nanoseconds.

FromSeconds(double)

Returns a Duration that represents the given number of seconds.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromSeconds(double seconds)
Parameters
Type Name Description
double seconds The number of seconds.
Returns
Type Description
Duration A Duration representing the given number of seconds.

FromSeconds(long)

Returns a Duration that represents the given number of seconds.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromSeconds(long seconds)
Parameters
Type Name Description
long seconds The number of seconds.
Returns
Type Description
Duration A Duration representing the given number of seconds.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromSeconds(42);
Console.WriteLine(duration.Seconds);
Console.WriteLine(duration.ToString());

Output:

42
0:00:00:42

FromTicks(double)

Returns a Duration that represents the given number of ticks.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromTicks(double ticks)
Parameters
Type Name Description
double ticks The number of ticks.
Returns
Type Description
Duration A Duration representing the given number of ticks.

FromTicks(long)

Returns a Duration that represents the given number of ticks.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration FromTicks(long ticks)
Parameters
Type Name Description
long ticks The number of ticks.
Returns
Type Description
Duration A Duration representing the given number of ticks.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromTicks(10_000_000);
Console.WriteLine(duration.TotalTicks);
Console.WriteLine(duration.TotalSeconds);
Console.WriteLine(duration.ToString());

Output:

10000000
1
0:00:00:01

FromTimeSpan(TimeSpan)

Returns a Duration that represents the same number of ticks as the given TimeSpan.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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.
Sample snippet
using NodaTime;
using System;

Duration duration = Duration.FromTimeSpan(TimeSpan.FromHours(3));
Console.WriteLine(duration.Hours);
Console.WriteLine(duration.ToString());

Output:

3
0:03:00:00

GetHashCode()

Returns a hash code for this instance. See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override int GetHashCode()
Returns
Type Description
int A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
Overrides
ValueType.GetHashCode()

Max(Duration, Duration)

Returns the larger duration of the given two.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Max(Duration x, Duration y)
Parameters
Type Name Description
Duration x The first duration to compare.
Duration y The second duration to compare.
Returns
Type Description
Duration The larger duration of x or y.
Remarks
A "larger" duration is one that advances time by more than a "smaller" one. This means that a positive duration is always larger than a negative one, for example. (This is the same comparison used by the binary comparison operators.)

Min(Duration, Duration)

Returns the smaller duration of the given two.
Since 2.3.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Min(Duration x, Duration y)
Parameters
Type Name Description
Duration x The first duration to compare.
Duration y The second duration to compare.
Returns
Type Description
Duration The smaller duration of x or y.
Remarks
A "larger" duration is one that advances time by more than a "smaller" one. This means that a positive duration is always larger than a negative one, for example. (This is the same comparison used by the binary comparison operators.)

Minus(Duration)

Returns the result of subtracting another duration from this one, for a fluent alternative to operator-().
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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, double)

Multiplies a duration by a number. Friendly alternative to operator*().
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Multiply(Duration left, double right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
double right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the product of the given values.

Multiply(Duration, long)

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

Multiply(double, Duration)

Multiplies a duration by a number. Friendly alternative to operator*().
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Multiply(double left, Duration right)
Parameters
Type Name Description
double 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.

Multiply(long, Duration)

Multiplies a duration by a number. Friendly alternative to operator*().
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration Multiply(long left, Duration right)
Parameters
Type Name Description
long 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 net6.0, net8.0, netstandard2.0
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 net6.0, net8.0, netstandard2.0
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 net6.0, net8.0, netstandard2.0
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.

ToBigIntegerNanoseconds()

Conversion to a BigInteger number of nanoseconds, as a convenient built-in numeric type which can always represent values in the range we need.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public BigInteger ToBigIntegerNanoseconds()
Returns
Type Description
BigInteger This duration as a number of nanoseconds, represented as a BigInteger.

ToInt128Nanoseconds()

Conversion to a Int128 number of nanoseconds, as a convenient built-in numeric type which can always represent values in the range we need.
Since 3.2.x
Availability net8.0
Declaration
public Int128 ToInt128Nanoseconds()
Returns
Type Description
Int128 This duration as a number of nanoseconds, represented as a BigInteger.

ToInt64Nanoseconds()

Conversion to an long number of nanoseconds. This will fail if the number of nanoseconds is out of the range of Int64, which is approximately 292 years (positive or negative).
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public long ToInt64Nanoseconds()
Returns
Type Description
long This duration as a number of nanoseconds, represented as an Int64.
Exceptions
Type Condition
OverflowException The number of nanoseconds is outside the representable range.

ToString()

Returns a string that represents this instance.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public override string ToString()
Returns
Type Description
string The value of the current instance in the default format pattern ("o"), using the current thread's culture to obtain a format provider.
Overrides
ValueType.ToString()

ToString(string?, IFormatProvider?)

Formats the value of the current instance using the specified pattern.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public string ToString(string? patternText, IFormatProvider? formatProvider)
Parameters
Type Name Description
string patternText The string specifying the pattern to use, or null to use the default format pattern ("o").
IFormatProvider formatProvider The IFormatProvider to use when formatting the value, or null to use the current thread's culture to obtain a format provider.
Returns
Type Description
string A string containing the value of the current instance in the specified format.

ToTimeSpan()

Returns a TimeSpan that represents the same number of ticks as this Duration.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public TimeSpan ToTimeSpan()
Returns
Type Description
TimeSpan A new TimeSpan with the same number of ticks as this Duration.
Remarks
If the number of nanoseconds in a duration is not a whole number of ticks, it is truncated towards zero. For example, durations in the range [-99ns, 99ns] would all count as 0 ticks.
Exceptions
Type Condition
OverflowException The number of ticks cannot be represented a signed 64-bit integer.

Operators

operator +(Duration, Duration)

Implements the operator + (addition).
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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.

operator /(Duration, Duration)

Implements the operator / (division) to divide one duration by another.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static double 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
double The double representing the result of dividing left by right.

operator /(Duration, double)

Implements the operator / (division) to divide a duration by a double.
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator /(Duration left, double right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
double right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of dividing left by right.

operator /(Duration, long)

Implements the operator / (division) to divide a duration by an long.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator /(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
long right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of dividing left by right.

operator ==(Duration, Duration)

Implements the operator == (equality). See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if values are equal to each other, otherwise false.

operator >(Duration, Duration)

Implements the operator > (greater than). See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if the left value is greater than the right value, otherwise false.

operator >=(Duration, Duration)

Implements the operator >= (greater than or equal). See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if the left value is greater than or equal to the right value, otherwise false.

operator !=(Duration, Duration)

Implements the operator != (inequality). See the type documentation for a description of equality semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if values are not equal to each other, otherwise false.

operator <(Duration, Duration)

Implements the operator < (less than). See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if the left value is less than the right value, otherwise false.

operator <=(Duration, Duration)

Implements the operator <= (less than or equal). See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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
bool true if the left value is less than or equal to the right value, otherwise false.

operator *(Duration, double)

Implements the operator * (multiplication).
Since 2.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator *(Duration left, double right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
double right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of multiplying left by right.

operator *(Duration, long)

Implements the operator * (multiplication).
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator *(Duration left, long right)
Parameters
Type Name Description
Duration left The left hand side of the operator.
long right The right hand side of the operator.
Returns
Type Description
Duration A new Duration representing the result of multiplying left by right.

operator *(double, Duration)

Implements the operator * (multiplication).
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator *(double left, Duration right)
Parameters
Type Name Description
double 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.

operator *(long, Duration)

Implements the operator * (multiplication).
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator *(long left, Duration right)
Parameters
Type Name Description
long 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.

operator -(Duration, Duration)

Implements the operator - (subtraction).
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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.

operator -(Duration)

Implements the unary negation operator.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
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

operator +(Duration)

Implements the operator + (unary).
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
public static Duration operator +(Duration duration)
Parameters
Type Name Description
Duration duration The duration.
Returns
Type Description
Duration The same duration Duration as provided.

Explicit Interface Implementations

IComparable.CompareTo(object?)

Implementation of CompareTo(object) to compare two durations. See the type documentation for a description of ordering semantics.
Since 1.0.x
Availability net6.0, net8.0, netstandard2.0
Declaration
int IComparable.CompareTo(object? obj)
Parameters
Type Name Description
object obj The object to compare this value with.
Returns
Type Description
int 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.

IXmlSerializable.GetSchema()

This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the XmlSchemaProviderAttribute to the class.
Since 3.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
XmlSchema IXmlSerializable.GetSchema()
Returns
Type Description
XmlSchema An XmlSchema that describes the XML representation of the object that is produced by the WriteXml(XmlWriter) method and consumed by the ReadXml(XmlReader) method.

IXmlSerializable.ReadXml(XmlReader)

Generates an object from its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.ReadXml(XmlReader reader)
Parameters
Type Name Description
XmlReader reader The XmlReader stream from which the object is deserialized.

IXmlSerializable.WriteXml(XmlWriter)

Converts an object into its XML representation.
Since 1.2.x
Availability net6.0, net8.0, netstandard2.0
Declaration
void IXmlSerializable.WriteXml(XmlWriter writer)
Parameters
Type Name Description
XmlWriter writer The XmlWriter stream to which the object is serialized.

Implements

IEquatable<T>
IComparable<T>
IComparable
IXmlSerializable
IFormattable
IAdditionOperators<TSelf, TOther, TResult>
ISubtractionOperators<TSelf, TOther, TResult>
IUnaryNegationOperators<TSelf, TResult>
IUnaryPlusOperators<TSelf, TResult>
IComparisonOperators<TSelf, TOther, TResult>
IEqualityOperators<TSelf, TOther, TResult>
IMinMaxValue<TSelf>
IAdditiveIdentity<TSelf, TResult>
In this article
Back to top Generated by DocFX