Export (0) Print
Expand All

TimeSpan Structure

Represents a time interval.

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

'Declaration
<ComVisibleAttribute(True)> _
Public Structure TimeSpan _
	Implements IComparable, IComparable(Of TimeSpan),  _
	IEquatable(Of TimeSpan), IFormattable

The TimeSpan type exposes the following members.

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360TimeSpan(Int64)Initializes a new TimeSpan to the specified number of ticks.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360TimeSpan(Int32, Int32, Int32)Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360TimeSpan(Int32, Int32, Int32, Int32)Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360TimeSpan(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 Silverlight for Windows PhoneSupported by Xbox 360DaysGets the days component of the time interval represented by the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360HoursGets the hour component of the time interval represented by the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360MillisecondsGets the millisecond component of the time interval represented by the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360MinutesGets the minute component of the time interval represented by the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360SecondsGets the second component of the time interval represented by the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TicksGets the number of ticks that represents the value of the current TimeSpan structure.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TotalDaysGets the value of the current TimeSpan structure expressed in whole and fractional days.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TotalHoursGets the value of the current TimeSpan structure expressed in whole and fractional hours.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TotalMillisecondsGets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TotalMinutesGets the value of the current TimeSpan structure expressed in whole and fractional minutes.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360TotalSecondsGets the value of the current TimeSpan structure expressed in whole and fractional seconds.
Top

  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360AddReturns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360CompareCompares two TimeSpan values and returns an integer that indicates whether the first time interval is longer than, equal to, or shorter than the second time interval.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CompareTo(Object)Compares this instance to a specified object and returns an integer that indicates whether the time interval represented by this instance is longer than, equal to, or shorter than the time interval represented by the specified object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CompareTo(TimeSpan)Compares this instance to a specified TimeSpan object and returns an integer that indicates whether the time interval represented by this instance is longer than, equal to, or shorter than the time interval represented by the specified TimeSpan object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360DurationReturns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Returns a value indicating whether this instance is equal to a specified object. (Overrides ValueType.Equals(Object).)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(TimeSpan)Returns a value indicating whether this instance is equal to a specified TimeSpan object.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(TimeSpan, TimeSpan)Returns a value indicating whether two specified instances of TimeSpan are equal.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromDaysReturns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromHoursReturns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromMillisecondsReturns a TimeSpan that represents a specified number of milliseconds.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromMinutesReturns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromSecondsReturns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360FromTicksReturns a TimeSpan that represents a specified time, where the specification is in units of ticks.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeReturns a hash code for this instance. (Overrides ValueType.GetHashCode.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360NegateReturns a new TimeSpan whose value is the negated value of this instance.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360Parse(String)Converts the string representation of a time interval to its TimeSpan equivalent.
Public methodStatic memberSupported by Silverlight for Windows PhoneParse(String, IFormatProvider)Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information.
Public methodStatic memberSupported by Silverlight for Windows PhoneParseExact(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 memberSupported by Silverlight for Windows PhoneParseExact(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 memberSupported by Silverlight for Windows PhoneParseExact(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 memberSupported by Silverlight for Windows PhoneParseExact(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 Silverlight for Windows PhoneSupported by Xbox 360SubtractReturns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringConverts the value of the current TimeSpan object to its equivalent string representation. (Overrides ValueType.ToString.)
Public methodSupported by Silverlight for Windows PhoneToString(String)Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format.
Public methodSupported by Silverlight for Windows PhoneToString(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 Silverlight for Windows PhoneSupported by Xbox 360TryParse(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 memberSupported by Silverlight for Windows PhoneTryParse(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 memberSupported by Silverlight for Windows PhoneTryParseExact(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 memberSupported by Silverlight for Windows PhoneTryParseExact(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 memberSupported by Silverlight for Windows PhoneTryParseExact(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 memberSupported by Silverlight for Windows PhoneTryParseExact(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 Silverlight for Windows PhoneSupported by Xbox 360AdditionAdds two specified TimeSpan instances.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360EqualityIndicates whether two TimeSpan instances are equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GreaterThanIndicates whether a specified TimeSpan is greater than another specified TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360GreaterThanOrEqualIndicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360InequalityIndicates whether two TimeSpan instances are not equal.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360LessThanIndicates whether a specified TimeSpan is less than another specified TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360LessThanOrEqualIndicates whether a specified TimeSpan is less than or equal to another specified TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360SubtractionSubtracts a specified TimeSpan from another specified TimeSpan.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360UnaryNegationReturns a TimeSpan whose value is the negated value of the specified instance.
Public operatorStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360UnaryPlusReturns the specified instance of TimeSpan.
Top

  NameDescription
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360MaxValueRepresents the maximum TimeSpan value. This field is read-only.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360MinValueRepresents the minimum TimeSpan value. This field is read-only.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TicksPerDayRepresents the number of ticks in 1 day. This field is constant.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TicksPerHourRepresents the number of ticks in 1 hour. This field is constant.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TicksPerMillisecondRepresents the number of ticks in 1 millisecond. This field is constant.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TicksPerMinuteRepresents the number of ticks in 1 minute. This field is constant.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360TicksPerSecondRepresents the number of ticks in 1 second.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360ZeroRepresents 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.

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 TimeSpan object is the number of ticks that equal the represented time interval. A tick is equal to 100 nanoseconds, and 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.

    
    Dim interval As New TimeSpan()
    outputBlock.Text &= interval.Equals(TimeSpan.Zero) & vbCrLf     ' Displays "True".
    
    
    
  • By calling one of its explicit constructors. The following example initializes a TimeSpan value to a specified number of hours, minutes, and seconds.

    
    Dim interval As New TimeSpan(2, 14, 18)
    outputBlock.Text &= interval.ToString() & vbCrLf                ' Displays "02:14:18".
    
    
    
  • 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.

    
    Dim departure As DateTime = #6/12/2010 6:32:00 PM#
    Dim arrival As DateTime = #6/13/2010 10:47:00 PM#
    Dim travelTime As TimeSpan = arrival - departure
    outputBlock.Text += String.Format("{0} - {1} = {2}", arrival, departure, travelTime) & vbCrLf
    ' The example displays the following output:
    '       6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
    
    
    

    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.

    
    Dim values() As String = {"12", "31.", "5.8:32:16", "12:12:15.95", ".12"}
    For Each value As String In values
       Try
          Dim ts As TimeSpan = TimeSpan.Parse(value)
          outputBlock.Text += String.Format("'{0}' --> {1}", value, ts) & vbCrLf
       Catch e As FormatException
          outputBlock.Text += String.Format("Unable to parse '{0}'", value) & vbCrLf
       Catch e As OverflowException
          outputBlock.Text += String.Format("'{0}' is outside the range of a TimeSpan.", value) & vbCrLf
       End Try
    Next
    ' The example displays the following output:
    '       '12' --> 12.00:00:00
    '       Unable to parse '31.'
    '       '5.8:32:16' --> 5.08:32:16
    '       '12:12:15.95' --> 12:12:15.9500000
    '       Unable to parse '.12'  
    
    
    

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. For example, the text representation of a TimeSpan object initialized to 1.0e+13 ticks is "11.13:46:40", which means 11 days, 13 hours, 46 minutes, and 40 seconds.

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


outputBlock.FontFamily = New System.Windows.Media.FontFamily("Courier New")

' Define two dates.
Dim date1 As Date = #1/1/2010 8:00:15 AM#
Dim date2 As Date = #8/18/2010 1:30:30 PM#
' Calculate the interval between the two dates.
Dim interval As TimeSpan = date2 - date1
outputBlock.Text += String.Format("{0} - {1} = {2}", date2, date1, interval.ToString()) & vbCrLf
' Display individual properties of the resulting TimeSpan object.
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Value of Days Component:", interval.Days) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Value of Hours Component:", interval.Hours) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds) & vbCrLf
outputBlock.Text += String.Format("   {0,-35} {1,20:N0}", "Ticks:", interval.Ticks) & vbCrLf
' The example displays the following output:
'       8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
'          Value of Days Component:                             229
'          Total Number of Days:                   229.229340277778
'          Value of Hours Component:                              5
'          Total Number of Hours:                  5501.50416666667
'          Value of Minutes Component:                           30
'          Total Number of Minutes:                       330090.25
'          Value of Seconds Component:                           15
'          Total Number of Seconds:                      19,805,415
'          Value of Milliseconds Component:                       0
'          Total Number of Milliseconds:             19,805,415,000
'          Ticks:                               198,054,150,000,000


Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

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

Show:
© 2014 Microsoft