Updated: August 2009
Represents a time interval.
Assembly: mscorlib (in mscorlib.dll)
A object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. The structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. Otherwise, the DateTime or DateTimeOffset structure should be used instead.
The largest unit of time used to measure duration is a day. Time intervals are measured in days for consistency, because the number of days in larger units of time, such as months and years, varies.
The value of a object is the number of ticks that equal the represented time interval. A tick is equal to 100 nanoseconds, and the value of a object can range from TimeSpan.MinValue to TimeSpan.MaxValue.
Instantiating a TimeSpan Value
You can instantiate a value in a number of ways:
By calling its implicit default constructor. This creates an object whose value is TimeSpan.Zero, as the following example shows.
By calling one of its explicit constructors. The following example initializes a value to a specified number of hours, minutes, and seconds.
By calling a method or performing an operation that returns a value. For example, you can instantiate a value that represents the interval between two date and time values, as the following example shows.
By parsing the string representation of a value. You can use the Parse and TryParse methods to convert strings that contain time intervals to values. The following example uses the Parse method to convert an array of strings to values.
Formatting a TimeSpan Value
A value can be represented as [-]d.hh:mm:ss.ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. That is, a time interval consists of a positive or negative number of days without a time of day, or a number of days with a time of day, or only a time of day. For example, the text representation of a object initialized to 1.0e+13 ticks is "11.13:46:40", which means 11 days, 13 hours, 46 minutes, and 40 seconds.
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.