[Serializable] |
A TimeSpan can be represented as a string in the format "[-]d.hh:mm:ss.ff" where "-" is an optional sign for negative TimeSpan values, the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. For example, a TimeSpan initialized with 1012 ticks would be represented as "11.13:46:40", which is 11 days, 13 hours, 46 minutes, and 40 seconds.
Due to a varying number of days in months and years, the longest unit of time that is used by TimeSpan is the day.
This value type implements the IComparable interface.
ctor #1 | Overloaded:.ctor(long ticks) Initializes a new TimeSpan to the specified number of ticks. |
ctor #2 | Overloaded:.ctor(int hours, int minutes, int seconds) Initializes a new TimeSpan to a specified number of hours, minutes, and seconds. |
ctor #3 | Overloaded:.ctor(int days, int hours, int minutes, int seconds) Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds. |
ctor #4 | Overloaded:.ctor(int days, int hours, int minutes, int seconds, int milliseconds) Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds. |
MaxValue | Represents the maximum TimeSpan value. This field is read-only. |
MinValue | Represents the minimum TimeSpan value. This field is read-only. |
TicksPerDay | Represents the number of ticks in 1 day. This field is constant. |
TicksPerHour | Represents the number of ticks in 1 hour. This field is constant. |
TicksPerMillisecond | Represents the number of ticks in 1 millisecond. This field is constant. |
TicksPerMinute | Represents the number of ticks in 1 minute. This field is constant. |
TicksPerSecond | Represents the number of ticks in 1 second. |
Zero | Represents the zero TimeSpan value. This field is read-only. |
Days | Read-only Gets the number of whole days represented by this instance. |
Hours | Read-only Gets the number of whole hours represented by this instance. |
Milliseconds | Read-only Gets the number of whole milliseconds represented by this instance. |
Minutes | Read-only Gets the number of whole minutes represented by this instance. |
Seconds | Read-only Gets the number of whole seconds represented by this instance. |
Ticks | Read-only Gets the value of this instance in ticks. |
TotalDays | Read-only Gets the value of this instance expressed in whole and fractional days. |
TotalHours | Read-only Gets the value of this instance expressed in whole and fractional hours. |
TotalMilliseconds | Read-only Gets the value of this instance expressed in whole and fractional milliseconds. |
TotalMinutes | Read-only Gets the value of this instance expressed in whole and fractional minutes. |
TotalSeconds | Read-only Gets the value of this instance expressed in whole and fractional seconds. |
Add | Adds the specified TimeSpan to this instance. |
Compare | Compares two TimeSpan values and returns an integer that indicates their relationship. |
CompareTo | Compares this instance to a specified object and returns an indication of their relative values. |
Duration | Returns a TimeSpan whose value is the absolute value of this instance. |
Equals | Overloaded:Equals(object value) Overridden: Returns a value indicating whether this instance is equal to a specified object. |
Equals | Overloaded:Equals(TimeSpan t1, TimeSpan t2) Returns a value indicating whether two specified instances of TimeSpan are equal. |
FromDays | Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond. |
FromHours | Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond. |
FromMilliseconds | Returns a TimeSpan that represents a specified number of milliseconds. |
FromMinutes | Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. |
FromSeconds | Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond. |
FromTicks | Returns a TimeSpan that represents a specified time, where the specification is in units of ticks. |
GetHashCode | Overridden: Returns a hash code for this instance. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Negate | Returns a TimeSpan whose value is the negated value of this instance. |
Parse | Constructs a TimeSpan from a time indicated by a specified string. |
Subtract | Subtracts the specified TimeSpan from this instance. |
ToString | Overridden: Returns the string representation of the value of this instance. |
op_Addition | Adds two specified TimeSpan instances. |
op_Equality | Indicates whether two TimeSpan instances are equal. |
op_GreaterThan | Indicates whether a specified TimeSpan is greater than another specified TimeSpan. |
op_GreaterThanOrEqual | Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan. |
op_Inequality | Indicates whether two TimeSpan instances are not equal. |
op_LessThan | Indicates whether a specified TimeSpan is less than another specified TimeSpan. |
op_LessThanOrEqual | Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan. |
op_Subtraction | Subtracts a specified TimeSpan from another specified TimeSpan. |
op_UnaryNegation | Returns a TimeSpan whose value is the negated value of the specified instance. |
op_UnaryPlus | Returns the specified instance of TimeSpan. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public TimeSpan( |
ticks
hours
minutes
seconds
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
This constructor accepts TimeSpan values in the range of TimeSpan.MinValue + 5808 ticks (that is, negative 10675199.02:48:05.477) to TimeSpan.MaxValue - 5808 ticks (that is, positive 10675199.02:48:05.477).
days
hours
minutes
seconds
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
This constructor accepts TimeSpan values in the range of TimeSpan.MinValue + 5808 ticks (that is, negative 10675199.02:48:05.477) to TimeSpan.MaxValue - 5808 ticks (that is, positive 10675199.02:48:05.477).
days
hours
minutes
seconds
milliseconds
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
This constructor accepts TimeSpan values in the range of TimeSpan.MinValue + 5808 ticks (that is, negative 10675199.02:48:05.477) to TimeSpan.MaxValue - 5808 ticks (that is, positive 10675199.02:48:05.477).
public static readonly TimeSpan MaxValue;
|
public static readonly TimeSpan MinValue;
|
public const long TicksPerDay;
|
public const long TicksPerHour;
|
public const long TicksPerMillisecond;
|
public const long TicksPerMinute;
|
public const long TicksPerSecond;
|
public static readonly TimeSpan Zero;
|
public int Days {get;}
|
public int Hours {get;}
|
public int Milliseconds {get;}
|
For example, the instruction,
Console.WriteLine("{0}",
System.TimeSpan.MaxValue.ToString());
yields the string, "10675199.02:48:05.4775807", where the "4775807" part is 477.5807 milliseconds. While the instruction,
Console.WriteLine("{0}",
System.TimeSpan.MaxValue.Milliseconds.ToString());
just yields the string, "477", which is 477 milliseconds.
public int Minutes {get;}
|
public int Seconds {get;}
|
public long Ticks {get;}
|
public double TotalDays {get;}
|
public double TotalHours {get;}
|
public double TotalMilliseconds {get;}
|
public double TotalMinutes {get;}
|
public double TotalSeconds {get;}
|
ts
Exception Type | Condition |
---|---|
OverflowException | The resulting TimeSpan is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
The return value is a new TimeSpan; the original TimeSpan is not modified.
t1
t2
Value | Condition |
---|---|
-1 | is less than |
0 | is equal to |
1 | is greater than |
value
Value | Condition |
---|---|
-1 | The value of this instance is less than the value of . |
0 | The value of this instance is equal to the value of . |
1 | The value of this instance is greater than the value of value. -or- value is null. |
Exception Type | Condition |
---|---|
ArgumentException | value is not a TimeSpan. |
The value parameter must be an instance of TimeSpan or null; otherwise, an exception is thrown.
public TimeSpan Duration(); |
Exception Type | Condition |
---|---|
OverflowException | The value of this instance is TimeSpan.MinValue. |
value
t1
t2
~TimeSpan(); |
value
Exception Type | Condition |
---|---|
OverflowException | value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
ArgumentException | value is equal to Double.NaN. |
If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.
value
Exception Type | Condition |
---|---|
OverflowException | value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
ArgumentException | value is equal to Double.NaN. |
If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.
value
Exception Type | Condition |
---|---|
OverflowException | value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
ArgumentException | value is equal to Double.NaN. |
If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.
value
Exception Type | Condition |
---|---|
OverflowException | value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
ArgumentException | value is equal to Double.NaN. |
If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.
value
Exception Type | Condition |
---|---|
OverflowException | value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
ArgumentException | value is equal to Double.NaN. |
If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.
value
public override int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public TimeSpan Negate(); |
Exception Type | Condition |
---|---|
OverflowException | The negated value of this instance cannot be represented by a TimeSpan; that is, the value of this instance is TimeSpan.MinValue. |
t1
t2
Exception Type | Condition |
---|---|
OverflowException | The resulting TimeSpan is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
t1
t2
t1
t2
t1
t2
t1
t2
t1
t2
t1
t2
t1
t2
Exception Type | Condition |
---|---|
OverflowException | The return value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
public static TimeSpan operator -( |
t
Exception Type | Condition |
---|---|
OverflowException | The negated value of this instance cannot be represented by a TimeSpan; that is, the value of this instance is TimeSpan.MinValue. |
public static TimeSpan operator +( |
t
s
Exception Type | Condition |
---|---|
ArgumentNullException | s is null. |
FormatException | s has an invalid format. |
OverflowException | s represents a number less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. -or- At least one of the hours, minutes, or seconds components is outside its valid range. |
[ws][-][d.]hh:mm:ss[.ff][ws]
Items in square brackets ([ and ]) are optional, colons and periods (: and .) are literal characters, and other items are as follows.
Item | Description |
---|---|
ws | optional white space |
"-" | optional minus sign indicating a negative time |
"d" | optional days |
"hh" | hours, ranging from 0 to 23 |
"mm" | minutes, ranging from 0 to 59 |
"ss" | seconds, ranging from 0 to 59 |
"ff" | optional fractional seconds, from 1 to 7 decimal digits |
ts
Exception Type | Condition |
---|---|
OverflowException | The return value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. |
The return value is a new TimeSpan; the original TimeSpan is not modified.
public override string ToString(); |
[-][d.]hh:mm:ss[.ff]
Items in square brackets ([ and ]) are optional, colons and periods (: and .) are literal characters; and the other items are as follows.
Item | Description |
---|---|
"-" | optional minus sign indicating a negative time |
"d" | optional days |
"hh" | hours, ranging from 0 to 23 |
"mm" | minutes, ranging from 0 to 59 |
"ss" | seconds, ranging from 0 to 59 |
"ff" | optional fractional seconds, from 1 to 7 decimal digits |