System.TimeSpan Structure

Assembly: Mscorlib.dll
Namespace: System
Summary
Represents a time interval.
C# Syntax:
[Serializable]
public struct TimeSpan : IComparable
Remarks
The value of an instance of TimeSpan represents a period of time. That value is the number of ticks contained in the instance and can range from Int64.MinValue to Int64.MaxValue. A tick is the smallest unit of time that can be specified, and is equal to 100 nanoseconds. Both the specification of a number of ticks and the value of a TimeSpan can be positive or negative.

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.

See also:
System Namespace | DateTime | Calendar

System.TimeSpan Member List:

Public Constructors
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.
Public Fields
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.
Public Properties
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.
Public Methods
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.
Public Operators and Type Conversions
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.
Protected Methods
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:


System.TimeSpan Member Details

Overloaded ctor #1
Summary
Initializes a new TimeSpan to the specified number of ticks.
C# Syntax:
public TimeSpan(
   long ticks
);
Parameters:

ticks

A time period expressed in 100-nanosecond units.

See also:
Int64

Return to top


Overloaded ctor #2
Summary
Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.
C# Syntax:
public TimeSpan(
   int hours,
   int minutes,
   int seconds
);
Parameters:

hours

Number of hours.

minutes

Number of minutes.

seconds

Number of seconds.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
Remarks
The specified hours, minutes, and seconds are converted to ticks, and that value initializes this instance.

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).

See also:
Int64

Return to top


Overloaded ctor #3
Summary
Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.
C# Syntax:
public TimeSpan(
   int days,
   int hours,
   int minutes,
   int seconds
);
Parameters:

days

Number of days.

hours

Number of hours.

minutes

Number of minutes.

seconds

Number of seconds.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
Remarks
The specified days, hours, minutes, and seconds are converted to ticks, and that value initializes this instance.

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).

See also:
Int64

Return to top


Overloaded ctor #4
Summary
Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.
C# Syntax:
public TimeSpan(
   int days,
   int hours,
   int minutes,
   int seconds,
   int milliseconds
);
Parameters:

days

Number of days.

hours

Number of hours.

minutes

Number of minutes.

seconds

Number of seconds.

milliseconds

Number of milliseconds.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException The parameters specify a TimeSpan value less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
Remarks
The specified days, hours, minutes, seconds, and milliseconds are converted to ticks, and that value initializes this instance.

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).

See also:
Int64

Return to top


Field: MaxValue
Summary
Represents the maximum TimeSpan value. This field is read-only.
C# Syntax:
public static readonly TimeSpan MaxValue;
Remarks
The value of this field is equivalent to Int64.MaxValue ticks. The string representation of this value is positive 10675199.02:48:05.4775807.
See also:
Int64 | TimeSpan.MinValue

Return to top


Field: MinValue
Summary
Represents the minimum TimeSpan value. This field is read-only.
C# Syntax:
public static readonly TimeSpan MinValue;
Remarks
The value of this field is equivalent to Int64.MinValue ticks. The string representation of this value is negative 10675199.02:48:05.4775808.
See also:
Int64 | TimeSpan.MaxValue

Return to top


Field: TicksPerDay
Summary
Represents the number of ticks in 1 day. This field is constant.
C# Syntax:
public const long TicksPerDay;
Remarks
The value of this constant is 864 billion; that is, 864,000,000,000.
See also:
Int64 | TimeSpan.TicksPerMillisecond | TimeSpan.TicksPerSecond | TimeSpan.TicksPerMinute | TimeSpan.TicksPerHour

Return to top


Field: TicksPerHour
Summary
Represents the number of ticks in 1 hour. This field is constant.
C# Syntax:
public const long TicksPerHour;
Remarks
The value of this constant is 36 billion; that is, 36,000,000,000.
See also:
Int64 | TimeSpan.TicksPerMillisecond | TimeSpan.TicksPerSecond | TimeSpan.TicksPerMinute | TimeSpan.TicksPerDay

Return to top


Field: TicksPerMillisecond
Summary
Represents the number of ticks in 1 millisecond. This field is constant.
C# Syntax:
public const long TicksPerMillisecond;
Remarks
The value of this constant is 10 thousand; that is, 10,000.
See also:
Int64 | TimeSpan.TicksPerSecond | TimeSpan.TicksPerMinute | TimeSpan.TicksPerHour | TimeSpan.TicksPerDay

Return to top


Field: TicksPerMinute
Summary
Represents the number of ticks in 1 minute. This field is constant.
C# Syntax:
public const long TicksPerMinute;
Remarks
The value of this constant is 600 million; that is, 600,000,000.
See also:
Int64 | TimeSpan.TicksPerMillisecond | TimeSpan.TicksPerSecond | TimeSpan.TicksPerHour | TimeSpan.TicksPerDay

Return to top


Field: TicksPerSecond
Summary
Represents the number of ticks in 1 second.
C# Syntax:
public const long TicksPerSecond;
Remarks
The value of this constant is 10 million; that is, 10,000,000.
See also:
Int64 | TimeSpan.TicksPerMillisecond | TimeSpan.TicksPerMinute | TimeSpan.TicksPerHour | TimeSpan.TicksPerDay

Return to top


Field: Zero
Summary
Represents the zero TimeSpan value. This field is read-only.
C# Syntax:
public static readonly TimeSpan Zero;
Remarks
This constant provides a convenient source for zero in time calculations.

Return to top


Property: Days (read-only)
Summary
Gets the number of whole days represented by this instance.
C# Syntax:
public int Days {get;}
Remarks
DateTime values can be represented as expressions of the form "d.hh:mm:ss.ff" where the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. The value of this property is the day component, "d".
See also:
TimeSpan.TotalDays | TimeSpan.Ticks | TimeSpan.Milliseconds | TimeSpan.Seconds | TimeSpan.Minutes | TimeSpan.Hours

Return to top


Property: Hours (read-only)
Summary
Gets the number of whole hours represented by this instance.
C# Syntax:
public int Hours {get;}
Remarks
DateTime values can be represented as expressions of the form "d.hh:mm:ss.ff" where the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. The value of this property is the hour component, "hh".
See also:
TimeSpan.TotalHours | TimeSpan.Ticks | TimeSpan.Milliseconds | TimeSpan.Seconds | TimeSpan.Minutes | TimeSpan.Days

Return to top


Property: Milliseconds (read-only)
Summary
Gets the number of whole milliseconds represented by this instance.
C# Syntax:
public int Milliseconds {get;}
Remarks
DateTime values can be represented as expressions of the form "d.hh:mm:ss.ff" where the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. The value of this property is the fractions-of-a-second component expressed in milliseconds.

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.

See also:
TimeSpan.TotalMilliseconds | TimeSpan.Ticks | TimeSpan.Seconds | TimeSpan.Minutes | TimeSpan.Hours | TimeSpan.Days

Return to top


Property: Minutes (read-only)
Summary
Gets the number of whole minutes represented by this instance.
C# Syntax:
public int Minutes {get;}
Remarks
DateTime values can be represented as expressions of the form "d.hh:mm:ss.ff" where the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. The value of this property is the minute component, "mm".
See also:
TimeSpan.TotalMinutes | TimeSpan.Ticks | TimeSpan.Milliseconds | TimeSpan.Seconds | TimeSpan.Hours | TimeSpan.Days

Return to top


Property: Seconds (read-only)
Summary
Gets the number of whole seconds represented by this instance.
C# Syntax:
public int Seconds {get;}
Remarks
DateTime values can be represented as expressions of the form "d.hh:mm:ss.ff" where the "d" component is days, "hh" is hours, "mm" is minutes, "ss" is seconds, and "ff" is fractions of a second. The value of this property is the second component, "ss".
See also:
TimeSpan.TotalSeconds | TimeSpan.Ticks | TimeSpan.Milliseconds | TimeSpan.Minutes | TimeSpan.Hours | TimeSpan.Days

Return to top


Property: Ticks (read-only)
Summary
Gets the value of this instance in ticks.
C# Syntax:
public long Ticks {get;}
Remarks
The smallest unit of time is the tick, which is equal to 100 nanoseconds. A tick can be negative or positive.
See also:
Int64 | TimeSpan.Milliseconds | TimeSpan.Seconds | TimeSpan.Minutes | TimeSpan.Hours | TimeSpan.Days

Return to top


Property: TotalDays (read-only)
Summary
Gets the value of this instance expressed in whole and fractional days.
C# Syntax:
public double TotalDays {get;}
Remarks
This property converts the value of this instance from ticks to days. This number might include whole and fractional days.
See also:
TimeSpan.Days | TimeSpan.TotalMilliseconds | TimeSpan.TotalSeconds | TimeSpan.TotalMinutes | TimeSpan.TotalHours

Return to top


Property: TotalHours (read-only)
Summary
Gets the value of this instance expressed in whole and fractional hours.
C# Syntax:
public double TotalHours {get;}
Remarks
This property converts the value of this instance from ticks to hours. This number might include whole and fractional hours.
See also:
TimeSpan.Hours | TimeSpan.TotalMilliseconds | TimeSpan.TotalSeconds | TimeSpan.TotalMinutes | TimeSpan.TotalDays

Return to top


Property: TotalMilliseconds (read-only)
Summary
Gets the value of this instance expressed in whole and fractional milliseconds.
C# Syntax:
public double TotalMilliseconds {get;}
Remarks
This property converts the value of this instance from ticks to milliseconds. This number might include whole and fractional milliseconds.
See also:
TimeSpan.Milliseconds | TimeSpan.TotalSeconds | TimeSpan.TotalMinutes | TimeSpan.TotalHours | TimeSpan.TotalDays

Return to top


Property: TotalMinutes (read-only)
Summary
Gets the value of this instance expressed in whole and fractional minutes.
C# Syntax:
public double TotalMinutes {get;}
Remarks
This property converts the value of this instance from ticks to minutes. This number might include whole and fractional minutes.
See also:
TimeSpan.Minutes | TimeSpan.TotalMilliseconds | TimeSpan.TotalSeconds | TimeSpan.TotalHours | TimeSpan.TotalDays

Return to top


Property: TotalSeconds (read-only)
Summary
Gets the value of this instance expressed in whole and fractional seconds.
C# Syntax:
public double TotalSeconds {get;}
Remarks
This property converts the value of this instance from ticks to seconds. This number might include whole and fractional seconds.
See also:
TimeSpan.Seconds | TimeSpan.TotalMilliseconds | TimeSpan.TotalMinutes | TimeSpan.TotalHours | TimeSpan.TotalDays

Return to top


Method: Add(
   TimeSpan ts
)
Summary
Adds the specified TimeSpan to this instance.
C# Syntax:
public TimeSpan Add(
   TimeSpan ts
);
Parameters:

ts

A TimeSpan.

Return Value:
A TimeSpan that represents the value of this instance plus the value of ts.
Exceptions
Exception Type Condition
OverflowException The resulting TimeSpan is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
Remarks
The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

The return value is a new TimeSpan; the original TimeSpan is not modified.

See also:
DateTime.Add

Return to top


Method: Compare(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Compares two TimeSpan values and returns an integer that indicates their relationship.
C# Syntax:
public static int Compare(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:


Value Condition
-1 is less than
0 is equal to
1 is greater than
See also:
TimeSpan.CompareTo | TimeSpan.Equals

Return to top


Method: CompareTo(
   object value
)
Summary
Compares this instance to a specified object and returns an indication of their relative values.
C# Syntax:
public int CompareTo(
   object value
);
Parameters:

value

An object to compare, or null.

Return 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.
Exceptions
Exception Type Condition
ArgumentException value is not a TimeSpan.
Remarks
Any instance of TimeSpan, regardless of its value, is considered greater than null.

The value parameter must be an instance of TimeSpan or null; otherwise, an exception is thrown.

See also:
Object | TimeSpan.Compare | TimeSpan.Equals

Return to top


Method: Duration()
Summary
Returns a TimeSpan whose value is the absolute value of this instance.
C# Syntax:
public TimeSpan Duration();
Return Value:
A TimeSpan whose value is the value of this instance and converted if necessary to a positive number.
Exceptions
Exception Type Condition
OverflowException The value of this instance is TimeSpan.MinValue.
Remarks
The value of a TimeSpan, which is the number of ticks it contains, can be positive or negative.
See also:
TimeSpan.Ticks

Return to top


Overloaded Method: Equals(
   object value
)
Summary
Returns a value indicating whether this instance is equal to a specified object.
C# Syntax:
public override bool Equals(
   object value
);
Parameters:

value

An object to compare with this instance.

Return Value:
true if value is a TimeSpan that represents the same time as this instance; otherwise, false.
See also:
Object | TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Overloaded Method: Equals(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Returns a value indicating whether two specified instances of TimeSpan are equal.
C# Syntax:
public static bool Equals(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the values of t1 and t2 are equal; otherwise, false.
See also:
Boolean | TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~TimeSpan();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: FromDays(
   double value
)
Summary
Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.
C# Syntax:
public static TimeSpan FromDays(
   double value
);
Parameters:

value

A number of days, accurate to the nearest millisecond.

Return Value:
A TimeSpan that represents value.
Exceptions
Exception Type Condition
OverflowException value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
ArgumentException value is equal to Double.NaN.
Remarks
The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond.

If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.

See also:
Double | TimeSpan.FromTicks | TimeSpan.FromMilliseconds | TimeSpan.FromSeconds | TimeSpan.FromMinutes | TimeSpan.FromHours

Return to top


Method: FromHours(
   double value
)
Summary
Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
C# Syntax:
public static TimeSpan FromHours(
   double value
);
Parameters:

value

A number of hours accurate to the nearest millisecond. A number of hours accurate to the nearest millisecond.

Return Value:
A TimeSpan that represents value.
Exceptions
Exception Type Condition
OverflowException value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
ArgumentException value is equal to Double.NaN.
Remarks
The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond.

If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.

See also:
Double | TimeSpan.FromTicks | TimeSpan.FromMilliseconds | TimeSpan.FromSeconds | TimeSpan.FromMinutes | TimeSpan.FromDays

Return to top


Method: FromMilliseconds(
   double value
)
Summary
Returns a TimeSpan that represents a specified number of milliseconds.
C# Syntax:
public static TimeSpan FromMilliseconds(
   double value
);
Parameters:

value

A number of milliseconds. A number of milliseconds.

Return Value:
A TimeSpan that represents value.
Exceptions
Exception Type Condition
OverflowException value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
ArgumentException value is equal to Double.NaN.
Remarks
The value parameter is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond.

If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.

See also:
Double | TimeSpan.FromTicks | TimeSpan.FromSeconds | TimeSpan.FromMinutes | TimeSpan.FromHours | TimeSpan.FromDays

Return to top


Method: FromMinutes(
   double value
)
Summary
Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
C# Syntax:
public static TimeSpan FromMinutes(
   double value
);
Parameters:

value

A number of minutes, accurate to the nearest millisecond. A number of minutes, accurate to the nearest millisecond.

Return Value:
A TimeSpan that represents value.
Exceptions
Exception Type Condition
OverflowException value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
ArgumentException value is equal to Double.NaN.
Remarks
The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond.

If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.

See also:
Double | TimeSpan.FromTicks | TimeSpan.FromMilliseconds | TimeSpan.FromSeconds | TimeSpan.FromHours | TimeSpan.FromDays

Return to top


Method: FromSeconds(
   double value
)
Summary
Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
C# Syntax:
public static TimeSpan FromSeconds(
   double value
);
Parameters:

value

A number of seconds, accurate to the nearest millisecond. A number of seconds, accurate to the nearest millisecond.

Return Value:
A TimeSpan that represents value.
Exceptions
Exception Type Condition
OverflowException value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
ArgumentException value is equal to Double.NaN.
Remarks
The value parameter is converted to milliseconds, which is converted to ticks, and that number of ticks is used to intialize the new TimeSpan. Therefore, value will only be considered accurate to the nearest millisecond.

If value is Double.PositiveInfinity, TimeSpan.MaxValue is returned. If value is Double.NegativeInfinity or Double.NaN, TimeSpan.MinValue is returned.

See also:
Double | TimeSpan.FromTicks | TimeSpan.FromMilliseconds | TimeSpan.FromMinutes | TimeSpan.FromHours | TimeSpan.FromDays

Return to top


Method: FromTicks(
   long value
)
Summary
Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.
C# Syntax:
public static TimeSpan FromTicks(
   long value
);
Parameters:

value

A number of ticks that represent a time.

Return Value:
A TimeSpan with a value of value.
Remarks
This is a convenience method with the same behavior as the TimeSpan.#ctor constructor.
See also:
Int64 | TimeSpan.FromMilliseconds | TimeSpan.FromSeconds | TimeSpan.FromMinutes | TimeSpan.FromHours | TimeSpan.FromDays

Return to top


Overridden Method: GetHashCode()
Summary
Returns a hash code for this instance.
C# Syntax:
public override int GetHashCode();
Return Value:
A 32-bit signed integer hash code.
Remarks
Two TimeSpan objects might have the same hash code even though they represent different time values.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Negate()
Summary
Returns a TimeSpan whose value is the negated value of this instance.
C# Syntax:
public TimeSpan Negate();
Return Value:
The same numeric value as this instance, but with the opposite sign.
Exceptions
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.

Return to top


Method: op_Addition(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Adds two specified TimeSpan instances.
C# Syntax:
public static TimeSpan operator +(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
A TimeSpan whose value is the sum of the values of t1 and t2.
Exceptions
Exception Type Condition
OverflowException The resulting TimeSpan is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

Return to top


Method: op_Equality(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether two TimeSpan instances are equal.
C# Syntax:
public static bool operator ==(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the values of t1 and t2 are equal; otherwise, false.
See also:
TimeSpan.Equals | TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_GreaterThan(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether a specified TimeSpan is greater than another specified TimeSpan.
C# Syntax:
public static bool operator >(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the value of t1 is greater than the value of t2; otherwise, false.
See also:
TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_GreaterThanOrEqual(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan.
C# Syntax:
public static bool operator >=(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the value of t1 is greater than or equal to the value of t2; otherwise, false.
See also:
TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_Inequality(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether two TimeSpan instances are not equal.
C# Syntax:
public static bool operator !=(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the values of t1 and t2 are not equal; otherwise, false.
See also:
TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_LessThan(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether a specified TimeSpan is less than another specified TimeSpan.
C# Syntax:
public static bool operator <(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the value of t1 is less than the value of t2; otherwise, false.
See also:
TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_LessThanOrEqual(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan.
C# Syntax:
public static bool operator <=(
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
true if the value of t1 is less than or equal to the value of t2; otherwise, false.
See also:
TimeSpan.Compare | TimeSpan.CompareTo

Return to top


Method: op_Subtraction(
   TimeSpan t1,
   TimeSpan t2
)
Summary
Subtracts a specified TimeSpan from another specified TimeSpan.
C# Syntax:
No member name (
   TimeSpan t1,
   TimeSpan t2
);
Parameters:

t1

A TimeSpan.

t2

A TimeSpan.

Return Value:
A TimeSpan whose value is the result of the value of t1 minus the value of t2.
Exceptions
Exception Type Condition
OverflowException The return value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.

Return to top


Method: op_UnaryNegation(
   TimeSpan t
)
Summary
Returns a TimeSpan whose value is the negated value of the specified instance.
C# Syntax:
public static TimeSpan operator -(
   TimeSpan t
);
Parameters:

t

A TimeSpan.

Return Value:
A TimeSpan with the same numeric value as this instance, but the opposite sign.
Exceptions
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.

Return to top


Method: op_UnaryPlus(
   TimeSpan t
)
Summary
Returns the specified instance of TimeSpan.
C# Syntax:
public static TimeSpan operator +(
   TimeSpan t
);
Parameters:

t

A TimeSpan.

Return Value:
Returns t.

Return to top


Method: Parse(
   string s
)
Summary
Constructs a TimeSpan from a time indicated by a specified string.
C# Syntax:
public static TimeSpan Parse(
   string s
);
Parameters:

s

A string.

Return Value:
A TimeSpan that corresponds to s.
Exceptions
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.

Remarks
The s parameter contains a specification of the form:

[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
See also:
String | TimeSpan.ToString

Return to top


Method: Subtract(
   TimeSpan ts
)
Summary
Subtracts the specified TimeSpan from this instance.
C# Syntax:
public TimeSpan Subtract(
   TimeSpan ts
);
Parameters:

ts

A TimeSpan.

Return Value:
A TimeSpan whose value is the result of the value of this instance minus the value of ts.
Exceptions
Exception Type Condition
OverflowException The return value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.
Remarks
The return value must be between TimeSpan.MinValue and TimeSpan.MaxValue; otherwise, an exception is thrown.

The return value is a new TimeSpan; the original TimeSpan is not modified.

See also:
DateTime.Subtract

Return to top


Overridden Method: ToString()
Summary
Returns the string representation of the value of this instance.
C# Syntax:
public override string ToString();
Return Value:
A string that represents the value of this instance. The format of the return value is of the form:

[-][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
Remarks
The return value of this method can be consumed by TimeSpan.Parse.
See also:
String | TimeSpan.Parse

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.