Export (0) Print
Expand All

TimeSpan Structure

Represents a time interval.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

[<Sealed>]
[<SerializableAttribute>]
[<ComVisibleAttribute(true)>]
type TimeSpan =  
    struct
        interface IComparable
        interface IComparable<TimeSpan>
        interface IEquatable<TimeSpan>
        interface IFormattable
    end

The TimeSpan type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryTimeSpan(Int64)Initializes a new TimeSpan to the specified number of ticks.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryTimeSpan(Int32, Int32, Int32)Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryTimeSpan(Int32, Int32, Int32, Int32)Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryTimeSpan(Int32, Int32, Int32, Int32, Int32)Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryDaysGets the days component of the time interval represented by the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryHoursGets the hours component of the time interval represented by the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryMillisecondsGets the milliseconds component of the time interval represented by the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryMinutesGets the minutes component of the time interval represented by the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySecondsGets the seconds component of the time interval represented by the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTicksGets the number of ticks that represent the value of the current TimeSpan structure.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTotalDaysGets the value of the current TimeSpan structure expressed in whole and fractional days.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTotalHoursGets the value of the current TimeSpan structure expressed in whole and fractional hours.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTotalMillisecondsGets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTotalMinutesGets the value of the current TimeSpan structure expressed in whole and fractional minutes.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryTotalSecondsGets the value of the current TimeSpan structure expressed in whole and fractional seconds.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryAddReturns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryCompareCompares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value.
Public methodSupported by the XNA FrameworkCompareTo(Object)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.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryCompareTo(TimeSpan)Compares this instance to a specified TimeSpan object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the TimeSpan object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryDurationReturns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(Object)Returns a value indicating whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(TimeSpan)Returns a value indicating whether this instance is equal to a specified TimeSpan object.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryEquals(TimeSpan, TimeSpan)Returns a value that indicates whether two specified instances of TimeSpan are equal.
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromDaysReturns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromHoursReturns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromMillisecondsReturns a TimeSpan that represents a specified number of milliseconds.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromMinutesReturns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromSecondsReturns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryFromTicksReturns a TimeSpan that represents a specified time, where the specification is in units of ticks.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns a hash code for this instance. (Overrides ValueType.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibraryMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryNegateReturns a new TimeSpan object whose value is the negated value of this instance.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryParse(String)Converts the string representation of a time interval to its TimeSpan equivalent.
Public methodStatic memberParse(String, IFormatProvider)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.
Public methodStatic memberParseExact(String, String, IFormatProvider)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.
Public methodStatic memberParseExact(String, String[], IFormatProvider)Converts the string representation of a time interval to its 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.
Public methodStatic memberParseExact(String, String, IFormatProvider, TimeSpanStyles)Converts the string representation of a time interval to its 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.
Public methodStatic memberParseExact(String, String[], IFormatProvider, TimeSpanStyles)Converts the string representation of a time interval to its 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.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySubtractReturns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToString()Converts the value of the current TimeSpan object to its equivalent string representation. (Overrides ValueType.ToString().)
Public methodToString(String)Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format.
Public methodToString(String, IFormatProvider)Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information.
Public methodStatic memberSupported by Portable Class LibraryTryParse(String, TimeSpan)Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberTryParse(String, IFormatProvider, TimeSpan)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.
Public methodStatic memberTryParseExact(String, String, IFormatProvider, TimeSpan)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match the specified format exactly.
Public methodStatic memberTryParseExact(String, String[], IFormatProvider, TimeSpan)Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match one of the specified formats exactly.
Public methodStatic memberTryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match the specified format exactly.
Public methodStatic memberTryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan)Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles, and returns a value that indicates whether the conversion succeeded. The format of the string representation must match one of the specified formats exactly.
Top

  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryAdditionAdds two specified TimeSpan instances.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryEqualityIndicates whether two TimeSpan instances are equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGreaterThanIndicates whether a specified TimeSpan is greater than another specified TimeSpan.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryGreaterThanOrEqualIndicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryInequalityIndicates whether two TimeSpan instances are not equal.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryLessThanIndicates whether a specified TimeSpan is less than another specified TimeSpan.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryLessThanOrEqualIndicates whether a specified TimeSpan is less than or equal to another specified TimeSpan.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySubtractionSubtracts a specified TimeSpan from another specified TimeSpan.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryUnaryNegationReturns a TimeSpan whose value is the negated value of the specified instance.
Public operatorStatic memberSupported by Portable Class LibraryUnaryPlusReturns the specified instance of TimeSpan.
Top

  NameDescription
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryMaxValueRepresents the maximum TimeSpan value. This field is read-only.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryMinValueRepresents the minimum TimeSpan value. This field is read-only.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryTicksPerDayRepresents the number of ticks in 1 day. This field is constant.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryTicksPerHourRepresents the number of ticks in 1 hour. This field is constant.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryTicksPerMillisecondRepresents the number of ticks in 1 millisecond. This field is constant.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryTicksPerMinuteRepresents the number of ticks in 1 minute. This field is constant.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryTicksPerSecondRepresents the number of ticks in 1 second.
Public fieldStatic memberSupported by the XNA FrameworkSupported by Portable Class LibraryZeroRepresents the zero TimeSpan value. This field is read-only.
Top

A TimeSpan 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 TimeSpan 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. (For more information about using the TimeSpan structure to reflect the time of day, see Choosing Between DateTime, DateTimeOffset, and TimeZoneInfo.)

The largest unit of time that the TimeSpan structure uses 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 TimeSpan object is the number of ticks that equal the represented time interval. A tick is equal to 100 nanoseconds, or one ten-millionth of a second. The value of a TimeSpan object can range from TimeSpan.MinValue to TimeSpan.MaxValue.

Instantiating a TimeSpan Value

You can instantiate a TimeSpan 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.

    No code example is currently available or this language may not be supported.
  • By calling one of its explicit constructors. The following example initializes a TimeSpan value to a specified number of hours, minutes, and seconds.

    No code example is currently available or this language may not be supported.
  • By calling a method or performing an operation that returns a TimeSpan value. For example, you can instantiate a TimeSpan value that represents the interval between two date and time values, as the following example shows.

    No code example is currently available or this language may not be supported.

    You can also initialize a TimeSpan object to a zero time value in this way, as the following example shows.

    No code example is currently available or this language may not be supported.

    TimeSpan values are returned by arithmetic operators and methods of the DateTime, DateTimeOffset, and TimeSpan structures.

  • By parsing the string representation of a TimeSpan value. You can use the Parse and TryParse methods to convert strings that contain time intervals to TimeSpan values. The following example uses the Parse method to convert an array of strings to TimeSpan values.

    No code example is currently available or this language may not be supported.

    In addition, you can define the precise format of the input string to be parsed and converted to a TimeSpan value by calling the ParseExact or TryParseExact method.

Performing Operations on TimeSpan Values

You can add and subtract time durations either by using the Addition and Subtraction operators, or by calling the Add and Subtract methods. You can also compare two time durations by calling the Compare, CompareTo, and Equals methods. The TimeSpan class also includes the Duration and Negate methods, which convert time intervals to positive and negative values,

The range of TimeSpan values is MinValue to MaxValue.

Formatting a TimeSpan Value

A TimeSpan 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.

Beginning with the .NET Framework version 4, the TimeSpan structure supports culture-sensitive formatting through the overloads of its ToString method, which converts a TimeSpan value to its string representation. The default TimeSpan.ToString() method returns a time interval by using an invariant format that is identical to its return value in previous versions of the .NET Framework. The TimeSpan.ToString(String) overload lets you specify a format string that defines the string representation of the time interval. The TimeSpan.ToString(String, IFormatProvider) overload lets you specify a format string and the culture whose formatting conventions are used to create the string representation of the time interval. TimeSpan supports both standard and custom format strings. (For more information, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.) However, only standard format strings are culture-sensitive.

Restoring Legacy TimeSpan Formatting

In some cases, code that successfully formats TimeSpan values in .NET Framework 3.5 and earlier versions fails in .NET Framework 4. This is most common in code that calls a composite formatting method to format a TimeSpan value with a format string. The following example successfully formats a TimeSpan value in .NET Framework 3.5 and earlier versions, but throws an exception in .NET Framework 4 and later versions. Note that it attempts to format a TimeSpan value by using an unsupported format specifier, which is ignored in .NET Framework 3.5 and earlier versions.

No code example is currently available or this language may not be supported.

If you cannot modify the code, you can restore the legacy formatting of TimeSpan values in one of the following ways:

  • By creating a configuration file that contains the <TimeSpan_LegacyFormatMode> element. Setting this element's enabled attribute to true restores legacy TimeSpan formatting on a per-application basis.

  • By setting the "NetFx40_TimeSpanLegacyFormatMode" compatibility switch when you create an application domain. This enables legacy TimeSpan formatting on a per-application-domain basis. The following example creates an application domain that uses legacy TimeSpan formatting.

    No code example is currently available or this language may not be supported.

    When the following code executes in the new application domain, it reverts to legacy TimeSpan formatting behavior.

    No code example is currently available or this language may not be supported.

The following example instantiates a TimeSpan object that represents the difference between two dates. It then displays the TimeSpan object's properties.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

ADD
Show:
© 2014 Microsoft