Early Preview
This is currently very much a preview. Please feel free to try things out,
but don't be upset if anything is not yet working. Feedback is welcome over on our
GitHub Dicussions page.
struct System.​TimeSpan
Assembly: System.Runtime
Implemented Interfaces
- IComparable
- IComparable`1
- IEquatable`1
- ISpanFormattable (Inherits: IFormattable)
- ISpanParsable`1 (Inherits: IParsable<​TimeSpan>)
- IUtf8SpanFormattable
Represents a time interval.
Fields and Constants
public const
int
HoursPerDay
Represents the number of hours in 1 day. This field is constant.
public static readonly
TimeSpan
MaxValue
Represents the maximum <see cref="T:System.TimeSpan" /> value. This field is read-only.
public const
long
MicrosecondsPerDay
Represents the number of microseconds in 1 day. This field is constant.
public const
long
MicrosecondsPerHour
Represents the number of microseconds in 1 hour. This field is constant.
public const
long
MicrosecondsPerMillisecond
Represents the number of microseconds in 1 millisecond. This field is constant.
public const
long
MicrosecondsPerMinute
Represents the number of microseconds in 1 minute. This field is constant.
public const
long
MicrosecondsPerSecond
Represents the number of microseconds in 1 second. This field is constant.
public const
long
MillisecondsPerDay
Represents the number of milliseconds in 1 day. This field is constant.
public const
long
MillisecondsPerHour
Represents the number of milliseconds in 1 hour. This field is constant.
public const
long
MillisecondsPerMinute
Represents the number of milliseconds in 1 minute. This field is constant.
public const
long
MillisecondsPerSecond
Represents the number of milliseconds in 1 second. This field is constant.
public const
long
MinutesPerDay
Represents the number of minutes in 1 day. This field is constant.
public const
long
MinutesPerHour
Represents the number of minutes in 1 hour. This field is constant.
public static readonly
TimeSpan
MinValue
Represents the minimum <see cref="T:System.TimeSpan" /> value. This field is read-only.
public const
long
NanosecondsPerTick
Represents the number of nanoseconds per tick. This field is constant.
public const
long
SecondsPerDay
Represents the number of seconds in 1 day. This field is constant.
public const
long
SecondsPerHour
Represents the number of seconds in 1 hour. This field is constant.
public const
long
SecondsPerMinute
Represents the number of seconds in 1 minute. This field is constant.
public const
long
TicksPerDay
Represents the number of ticks in 1 day. This field is constant.
public const
long
TicksPerHour
Represents the number of ticks in 1 hour. This field is constant.
public const
long
TicksPerMicrosecond
Represents the number of ticks in 1 microsecond. This field is constant.
public const
long
TicksPerMillisecond
Represents the number of ticks in 1 millisecond. This field is constant.
public const
long
TicksPerMinute
Represents the number of ticks in 1 minute. This field is constant.
public const
long
TicksPerSecond
Represents the number of ticks in 1 second.
public static readonly
TimeSpan
Zero
Represents the zero <see cref="T:System.TimeSpan" /> value. This field is read-only.
Properties
public
int
Days
Gets the days component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Hours
Gets the hours component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Microseconds
Gets the microseconds component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Milliseconds
Gets the milliseconds component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Minutes
Gets the minutes component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Nanoseconds
Gets the nanoseconds component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
int
Seconds
Gets the seconds component of the time interval represented by the current <see cref="T:System.TimeSpan" /> structure.
public
long
Ticks
Gets the number of ticks that represent the value of the current <see cref="T:System.TimeSpan" /> structure.
public
double
TotalDays
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional days.
public
double
TotalHours
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional hours.
public
double
TotalMicroseconds
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional microseconds.
public
double
TotalMilliseconds
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional milliseconds.
public
double
TotalMinutes
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional minutes.
public
double
TotalNanoseconds
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional nanoseconds.
public
double
TotalSeconds
Gets the value of the current <see cref="T:System.TimeSpan" /> structure expressed in whole and fractional seconds.
Methods
public
TimeSpan
Add​(TimeSpan ts)
Returns a new <see cref="T:System.TimeSpan" /> object whose value is the sum of the specified <see cref="T:System.TimeSpan" /> object and this instance.
Returns A new object that represents the value of this instance plus the value of <paramref name="ts" /> .
ts
The time interval to add.
public static
int
Compare​(TimeSpan t1,
TimeSpan t2)
Compares two <see cref="T:System.TimeSpan" /> values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value.
Returns One of the following values.
<list type="table">
<listheader>
<term> Value</term>
<description> Description</description>
</listheader>
<item>
<term> -1</term>
<description>
<paramref name="t1" /> is shorter than <paramref name="t2" />.</description>
</item>
<item>
<term> 0</term>
<description>
<paramref name="t1" /> is equal to <paramref name="t2" />.</description>
</item>
<item>
<term> 1</term>
<description>
<paramref name="t1" /> is longer than <paramref name="t2" />.</description>
</item>
</list>
t1
The first time interval to compare.
t2
The second time interval to compare.
public
int
CompareTo​(object value)
Compares this instance to a specified object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the specified object.
Returns One of the following values.
<list type="table">
<listheader>
<term> Value</term>
<description> Description</description>
</listheader>
<item>
<term> -1</term>
<description> This instance is shorter than <paramref name="value" />.</description>
</item>
<item>
<term> 0</term>
<description> This instance is equal to <paramref name="value" />.</description>
</item>
<item>
<term> 1</term>
<description> This instance is longer than <paramref name="value" />, or <paramref name="value" /> is <see langword="null" />.</description>
</item>
</list>
value
An object to compare, or <see langword="null" /> .
public
int
CompareTo​(TimeSpan value)
Compares this instance to a specified <see cref="T:System.TimeSpan" /> object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the <see cref="T:System.TimeSpan" /> object.
Returns A signed number indicating the relative values of this instance and <paramref name="value" /> .
<list type="table">
<listheader>
<term> Value</term>
<description> Description</description>
</listheader>
<item>
<term> A negative integer</term>
<description> This instance is shorter than <paramref name="value" />.</description>
</item>
<item>
<term> Zero</term>
<description> This instance is equal to <paramref name="value" />.</description>
</item>
<item>
<term> A positive integer</term>
<description> This instance is longer than <paramref name="value" />.</description>
</item>
</list>
value
An object to compare to this instance.
public
TimeSpan
Divide​(double divisor)
Returns a new <see cref="T:System.TimeSpan" /> object whose value is the result of dividing this instance by the specified <paramref name="divisor" /> .
Returns A new object that represents the value of this instance divided by the value of <paramref name="divisor" /> .
divisor
The divisor or value to be divided by.
public
double
Divide​(TimeSpan ts)
Returns a new <see cref="T:System.Double" /> value that's the result of dividing this instance by <paramref name="ts" /> .
Returns A new value that represents result of dividing this instance by the value of <paramref name="ts" /> .
ts
The value to be divided by.
public
TimeSpan
Duration​()
Returns a new <see cref="T:System.TimeSpan" /> object whose value is the absolute value of the current <see cref="T:System.TimeSpan" /> object.
Returns A new object whose value is the absolute value of the current <see cref="T:System.TimeSpan" /> object.
public
bool
Equals​(object value)
Returns a value indicating whether this instance is equal to a specified object.
Returns <see langword="true" /> if <paramref name="value" /> is a <see cref="T:System.TimeSpan" /> object that represents the same time interval as the current <see cref="T:System.TimeSpan" /> structure; otherwise, <see langword="false" /> .
value
An object to compare with this instance.
public
bool
Equals​(TimeSpan obj)
Returns a value indicating whether this instance is equal to a specified <see cref="T:System.TimeSpan" /> object.
Returns <see langword="true" /> if <paramref name="obj" /> represents the same time interval as this instance; otherwise, <see langword="false" /> .
obj
An object to compare with this instance.
public static
bool
Equals​(TimeSpan t1,
TimeSpan t2)
Returns a value that indicates whether two specified instances of <see cref="T:System.TimeSpan" /> are equal.
Returns <see langword="true" /> if the values of <paramref name="t1" /> and <paramref name="t2" /> are equal; otherwise, <see langword="false" /> .
t1
The first time interval to compare.
t2
The second time interval to compare.
public static
TimeSpan
FromDays​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of days, where the specification is accurate to the nearest millisecond.
Returns An object that represents <paramref name="value" /> .
value
A number of days, accurate to the nearest millisecond.
public static
TimeSpan
FromDays​(int days)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of days.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of days.
days
Number of days.
public static
TimeSpan
FromDays​(int days,
int hours = 0,
long minutes = 0,
long seconds = 0,
long milliseconds = 0,
long microseconds = 0)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of days, hours, minutes, seconds, milliseconds, and microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of days, hours, minutes, seconds, milliseconds, and microseconds.
days
Number of days.
hours
Number of hours.
minutes
Number of minutes.
seconds
Number of seconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromHours​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
Returns An object that represents <paramref name="value" /> .
value
A number of hours accurate to the nearest millisecond.
public static
TimeSpan
FromHours​(int hours)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of hours.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of hours.
hours
Number of hours.
public static
TimeSpan
FromHours​(int hours,
long minutes = 0,
long seconds = 0,
long milliseconds = 0,
long microseconds = 0)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of hours, minutes, seconds, milliseconds, and microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of hours, minutes, seconds, milliseconds, and microseconds.
hours
Number of hours.
minutes
Number of minutes.
seconds
Number of seconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromMicroseconds​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of microseconds.
Returns An object that represents <paramref name="value" /> .
value
A number of microseconds.
public static
TimeSpan
FromMicroseconds​(long microseconds)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of microseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromMilliseconds​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of milliseconds.
Returns An object that represents <paramref name="value" /> .
value
A number of milliseconds.
public static
TimeSpan
FromMilliseconds​(long milliseconds)
<para>Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of milliseconds.</para>
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of milliseconds.
milliseconds
The number of milliseconds.
public static
TimeSpan
FromMilliseconds​(long milliseconds,
long microseconds)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of milliseconds, and microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of milliseconds, and microseconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromMinutes​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
Returns An object that represents <paramref name="value" /> .
value
A number of minutes, accurate to the nearest millisecond.
public static
TimeSpan
FromMinutes​(long minutes)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of minutes.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of minutes.
minutes
Number of minutes.
public static
TimeSpan
FromMinutes​(long minutes,
long seconds = 0,
long milliseconds = 0,
long microseconds = 0)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of minutes, seconds, milliseconds, and microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of minutes, seconds, milliseconds, and microseconds.
minutes
Number of minutes.
seconds
Number of seconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromSeconds​(double value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
Returns An object that represents <paramref name="value" /> .
value
A number of seconds, accurate to the nearest millisecond.
public static
TimeSpan
FromSeconds​(long seconds)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of seconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of seconds.
seconds
Number of seconds.
public static
TimeSpan
FromSeconds​(long seconds,
long milliseconds = 0,
long microseconds = 0)
Initializes a new instance of the <see cref="T:System.TimeSpan" /> structure to a specified number of seconds, milliseconds, and microseconds.
Returns Returns a <see cref="T:System.TimeSpan" /> that represents a specified number of seconds, milliseconds, and microseconds.
seconds
Number of seconds.
milliseconds
Number of milliseconds.
microseconds
Number of microseconds.
public static
TimeSpan
FromTicks​(long value)
Returns a <see cref="T:System.TimeSpan" /> that represents a specified time, where the specification is in units of ticks.
Returns An object that represents <paramref name="value" /> .
value
A number of ticks that represent a time.
public
int
GetHashCode​()
Returns a hash code for this instance.
Returns A 32-bit signed integer hash code.
public
TimeSpan
Multiply​(double factor)
Returns a new <see cref="T:System.TimeSpan" /> object which value is the result of multiplication of this instance and the specified <paramref name="factor" /> .
Returns A new object that represents the value of this instance multiplied by the value of <paramref name="factor" /> .
factor
The value to be multiplied by.
public
TimeSpan
Negate​()
Returns a new <see cref="T:System.TimeSpan" /> object whose value is the negated value of this instance.
Returns A new object with the same numeric value as this instance, but with the opposite sign.
public static
TimeSpan
Parse​(ReadOnlySpan<​char> input,
IFormatProvider? formatProvider = null)
Converts the span representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified culture-specific format information.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="formatProvider" /> .
input
A span containing the characters that represent the time interval to convert.
formatProvider
An object that supplies culture-specific formatting information.
public static
TimeSpan
Parse​(string s)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent.
Returns A time interval that corresponds to <paramref name="s" /> .
s
A string that specifies the time interval to convert.
public static
TimeSpan
Parse​(string input,
IFormatProvider? formatProvider)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified culture-specific format information.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="formatProvider" /> .
input
A string that specifies the time interval to convert.
formatProvider
An object that supplies culture-specific formatting information.
public static
TimeSpan
ParseExact​(ReadOnlySpan<​char> input,
ReadOnlySpan<​char> format,
IFormatProvider? formatProvider,
TimeSpanStyles styles = 0)
Converts the char span of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="format" /> and <paramref name="formatProvider" /> .
input
A span that specifies the time interval to convert.
format
A standard or custom format string that defines the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
styles
A bitwise combination of enumeration values that defines the style elements that may be present in <paramref name="input" /> .
public static
TimeSpan
ParseExact​(ReadOnlySpan<​char> input,
string[] formats,
IFormatProvider? formatProvider,
TimeSpanStyles styles = 0)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified formats, culture-specific format information, and styles. The format of the string representation must match one of the specified formats exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="formats" /> , <paramref name="formatProvider" /> , and <paramref name="styles" /> .
input
A span that specifies the time interval to convert.
formats
An array of standard or custom format strings that define the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
styles
A bitwise combination of enumeration values that defines the style elements that may be present in input.
public static
TimeSpan
ParseExact​(string input,
string format,
IFormatProvider? formatProvider)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="format" /> and <paramref name="formatProvider" /> .
input
A string that specifies the time interval to convert.
format
A standard or custom format string that defines the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
public static
TimeSpan
ParseExact​(string input,
string format,
IFormatProvider? formatProvider,
TimeSpanStyles styles)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified format, culture-specific format information, and styles. The format of the string representation must match the specified format exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="format" /> , <paramref name="formatProvider" /> , and <paramref name="styles" /> .
input
A string that specifies the time interval to convert.
format
A standard or custom format string that defines the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
styles
A bitwise combination of enumeration values that defines the style elements that may be present in <paramref name="input" /> .
public static
TimeSpan
ParseExact​(string input,
string[] formats,
IFormatProvider? formatProvider)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified array of format strings and culture-specific format information. The format of the string representation must match one of the specified formats exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="formats" /> and <paramref name="formatProvider" /> .
input
A string that specifies the time interval to convert.
formats
An array of standard or custom format strings that defines the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
public static
TimeSpan
ParseExact​(string input,
string[] formats,
IFormatProvider? formatProvider,
TimeSpanStyles styles)
Converts the string representation of a time interval to its <see cref="T:System.TimeSpan" /> equivalent by using the specified formats, culture-specific format information, and styles. The format of the string representation must match one of the specified formats exactly.
Returns A time interval that corresponds to <paramref name="input" /> , as specified by <paramref name="formats" /> , <paramref name="formatProvider" /> , and <paramref name="styles" /> .
input
A string that specifies the time interval to convert.
formats
An array of standard or custom format strings that define the required format of <paramref name="input" /> .
formatProvider
An object that provides culture-specific formatting information.
styles
A bitwise combination of enumeration values that defines the style elements that may be present in input.
public
TimeSpan
Subtract​(TimeSpan ts)
Returns a new <see cref="T:System.TimeSpan" /> object whose value is the difference between the specified <see cref="T:System.TimeSpan" /> object and this instance.
Returns A new time interval whose value is the result of the value of this instance minus the value of <paramref name="ts" /> .
ts
The time interval to be subtracted.
public
string
ToString​()
Converts the value of the current <see cref="T:System.TimeSpan" /> object to its equivalent string representation.
Returns The string representation of the current <see cref="T:System.TimeSpan" /> value.
public
string
ToString​(string format)
Converts the value of the current <see cref="T:System.TimeSpan" /> object to its equivalent string representation by using the specified format.
Returns The string representation of the current <see cref="T:System.TimeSpan" /> value in the format specified by the <paramref name="format" /> parameter.
format
A standard or custom <see cref="T:System.TimeSpan" /> format string.
public
string
ToString​(string format,
IFormatProvider formatProvider)
Converts the value of the current <see cref="T:System.TimeSpan" /> object to its equivalent string representation by using the specified format and culture-specific formatting information.
Returns The string representation of the current <see cref="T:System.TimeSpan" /> value, as specified by <paramref name="format" /> and <paramref name="formatProvider" /> .
format
A standard or custom <see cref="T:System.TimeSpan" /> format string.
formatProvider
An object that supplies culture-specific formatting information.
public
bool
TryFormat​(Span<​byte> utf8Destination,
Int32& bytesWritten,
ReadOnlySpan<​char> format = null,
IFormatProvider? formatProvider = null)
public
bool
TryFormat​(Span<​char> destination,
Int32& charsWritten,
ReadOnlySpan<​char> format = null,
IFormatProvider? formatProvider = null)
public static
bool
TryParse​(ReadOnlySpan<​char> input,
IFormatProvider? formatProvider,
TimeSpan& result)
public static
bool
TryParse​(string input,
IFormatProvider formatProvider,
TimeSpan& result)
public static
bool
TryParseExact​(ReadOnlySpan<​char> input,
ReadOnlySpan<​char> format,
IFormatProvider? formatProvider,
TimeSpanStyles styles,
TimeSpan& result)
public static
bool
TryParseExact​(ReadOnlySpan<​char> input,
ReadOnlySpan<​char> format,
IFormatProvider? formatProvider,
TimeSpan& result)
public static
bool
TryParseExact​(ReadOnlySpan<​char> input,
string[] formats,
IFormatProvider formatProvider,
TimeSpanStyles styles,
TimeSpan& result)
public static
bool
TryParseExact​(ReadOnlySpan<​char> input,
string[] formats,
IFormatProvider formatProvider,
TimeSpan& result)
public static
bool
TryParseExact​(string input,
string format,
IFormatProvider formatProvider,
TimeSpanStyles styles,
TimeSpan& result)
public static
bool
TryParseExact​(string input,
string format,
IFormatProvider formatProvider,
TimeSpan& result)
public static
bool
TryParseExact​(string input,
string[] formats,
IFormatProvider formatProvider,
TimeSpanStyles styles,
TimeSpan& result)
public static
bool
TryParseExact​(string input,
string[] formats,
IFormatProvider formatProvider,
TimeSpan& result)
protected
void
Finalize​()
Inherited from object
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
public
Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected
object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .