TimeSpan Struct
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Represents a time interval.
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
public value class TimeSpan : IComparable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
[System.Serializable]
public struct TimeSpan : IComparable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
type TimeSpan = struct
interface IFormattable
type TimeSpan = struct
interface ISpanFormattable
interface IFormattable
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
interface IUtf8SpanFormattable
[<System.Serializable>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
interface IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), ISpanFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan)
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan), IUtf8SpanFormattable
Public Structure TimeSpan
Implements IComparable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan)
- Inheritance
- Attributes
- Implements
The following example instantiates a TimeSpan object that represents the difference between two dates. It then displays the TimeSpan object's properties.
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
// Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
// This 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
// Define two dates.
let date1 = DateTime(2010, 1, 1, 8, 0, 15)
let date2 = DateTime(2010, 8, 18, 13, 30, 30)
// Calculate the interval between the two dates.
let interval = date2 - date1
printfn $"{date2} - {date1} = {interval}"
// Display individual properties of the resulting TimeSpan object.
printfn $""" {"Value of Days Component:",-35} {interval.Days,20}"""
printfn $""" {"Total Number of Days:",-35} {interval.TotalDays,20}"""
printfn $""" {"Value of Hours Component:",-35} {interval.Hours,20}"""
printfn $""" {"Total Number of Hours:",-35} {interval.TotalHours,20}"""
printfn $""" {"Value of Minutes Component:",-35} {interval.Minutes,20}"""
printfn $""" {"Total Number of Minutes:",-35} {interval.TotalMinutes,20}"""
printfn $""" {"Value of Seconds Component:",-35} {interval.Seconds,20:N0}"""
printfn $""" {"Total Number of Seconds:",-35} {interval.TotalSeconds,20:N0}"""
printfn $""" {"Value of Milliseconds Component:",-35} {interval.Milliseconds,20:N0}"""
printfn $""" {"Total Number of Milliseconds:",-35} {interval.TotalMilliseconds,20:N0}"""
printfn $""" {"Ticks:",-35} {interval.Ticks,20:N0}"""
// This 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
' Define two dates.
Dim date1 As Date = #1/1/2010 8:00:15AM#
Dim date2 As Date = #8/18/2010 1:30:30PM#
' Calculate the interval between the two dates.
Dim interval As TimeSpan = date2 - date1
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString())
' Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays)
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours)
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks)
' 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
# Define two dates.
$Date2 = Get-Date -Date '2010/8/18' -Hour 13 -Minute 30 -Second 30
$Date1 = Get-Date -Date '2010/1/1' -Hour 8 -Minute 0 -Second 15
# Calculate the interval between the two dates.
$Interval = $Date2 - $Date1
"{0} - {1} = {2}" -f $Date2, $Date1, ($Interval.ToString())
# Display individual properties of the resulting TimeSpan object.
" {0,-35} {1,20}" -f "Value of Days Component:", $Interval.Days
" {0,-35} {1,20}" -f "Total Number of Days:", $Interval.TotalDays
" {0,-35} {1,20}" -f "Value of Hours Component:", $Interval.Hours
" {0,-35} {1,20}" -f "Total Number of Hours:", $Interval.TotalHours
" {0,-35} {1,20}" -f "Value of Minutes Component:", $Interval.Minutes
" {0,-35} {1,20}" -f "Total Number of Minutes:", $Interval.TotalMinutes
" {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $Interval.Seconds
" {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $Interval.TotalSeconds
" {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $Interval.Milliseconds
" {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $Interval.TotalMilliseconds
" {0,-35} {1,20:N0}" -f "Ticks:", $Interval.Ticks
<# This sample produces the following output:
18/08/2010 13:30:30 - 01/01/2010 08:00:15 = 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
#>
For more information about this API, see Supplemental API remarks for TimeSpan.
Time |
Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds, milliseconds, and microseconds. |
Time |
Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds, and milliseconds. |
Time |
Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, and seconds. |
Time |
Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds. |
Time |
Initializes a new instance of the TimeSpan structure to the specified number of ticks. |
Hours |
Represents the number of hours in 1 day. This field is constant. |
Max |
Represents the maximum TimeSpan value. This field is read-only. |
Microseconds |
Represents the number of microseconds in 1 day. This field is constant. |
Microseconds |
Represents the number of microseconds in 1 hour. This field is constant. |
Microseconds |
Represents the number of microseconds in 1 millisecond. This field is constant. |
Microseconds |
Represents the number of microseconds in 1 minute. This field is constant. |
Microseconds |
Represents the number of microseconds in 1 second. This field is constant. |
Milliseconds |
Represents the number of milliseconds in 1 day. This field is constant. |
Milliseconds |
Represents the number of milliseconds in 1 hour. This field is constant. |
Milliseconds |
Represents the number of milliseconds in 1 minute. This field is constant. |
Milliseconds |
Represents the number of milliseconds in 1 second. This field is constant. |
Minutes |
Represents the number of minutes in 1 day. This field is constant. |
Minutes |
Represents the number of minutes in 1 hour. This field is constant. |
Min |
Represents the minimum TimeSpan value. This field is read-only. |
Nanoseconds |
Represents the number of nanoseconds per tick. This field is constant. |
Seconds |
Represents the number of seconds in 1 day. This field is constant. |
Seconds |
Represents the number of seconds in 1 hour. This field is constant. |
Seconds |
Represents the number of seconds in 1 minute. This field is constant. |
Ticks |
Represents the number of ticks in 1 day. This field is constant. |
Ticks |
Represents the number of ticks in 1 hour. This field is constant. |
Ticks |
Represents the number of ticks in 1 microsecond. This field is constant. |
Ticks |
Represents the number of ticks in 1 millisecond. This field is constant. |
Ticks |
Represents the number of ticks in 1 minute. This field is constant. |
Ticks |
Represents the number of ticks in 1 second. |
Zero |
Represents the zero TimeSpan value. This field is read-only. |
Days |
Gets the days component of the time interval represented by the current TimeSpan structure. |
Hours |
Gets the hours component of the time interval represented by the current TimeSpan structure. |
Microseconds |
Gets the microseconds component of the time interval represented by the current TimeSpan structure. |
Milliseconds |
Gets the milliseconds component of the time interval represented by the current TimeSpan structure. |
Minutes |
Gets the minutes component of the time interval represented by the current TimeSpan structure. |
Nanoseconds |
Gets the nanoseconds component of the time interval represented by the current TimeSpan structure. |
Seconds |
Gets the seconds component of the time interval represented by the current TimeSpan structure. |
Ticks |
Gets the number of ticks that represent the value of the current TimeSpan structure. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional days. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional hours. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional microseconds. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional minutes. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional nanoseconds. |
Total |
Gets the value of the current TimeSpan structure expressed in whole and fractional seconds. |
Add(Time |
Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. |
Compare(Time |
Compares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. |
Compare |
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. |
Compare |
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. |
Divide(Double) |
Returns a new TimeSpan object whose value is the result of dividing this instance by the specified |
Divide(Time |
Returns a new Double value that's the result of dividing this instance by |
Duration() |
Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object. |
Equals(Object) |
Returns a value indicating whether this instance is equal to a specified object. |
Equals(Time |
Returns a value that indicates whether two specified instances of TimeSpan are equal. |
Equals(Time |
Returns a value indicating whether this instance is equal to a specified TimeSpan object. |
From |
Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds, milliseconds, and microseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of days. |
From |
Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, seconds, milliseconds, and microseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of hours. |
From |
Returns a TimeSpan that represents a specified number of microseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of microseconds. |
From |
Returns a TimeSpan that represents a specified number of milliseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of milliseconds, and microseconds. |
From |
Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of minutes, seconds, milliseconds, and microseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of minutes. |
From |
Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of seconds, milliseconds, and microseconds. |
From |
Initializes a new instance of the TimeSpan structure to a specified number of seconds. |
From |
Returns a TimeSpan that represents a specified time, where the specification is in units of ticks. |
Get |
Returns a hash code for this instance. |
Multiply(Double) |
Returns a new TimeSpan object which value is the result of multiplication of this instance and the specified |
Negate() |
Returns a new TimeSpan object whose value is the negated value of this instance. |
Parse(Read |
Converts the span representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information. |
Parse(String, IFormat |
Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information. |
Parse(String) |
Converts the string representation of a time interval to its TimeSpan equivalent. |
Parse |
Converts the char span 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. |
Parse |
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. |
Parse |
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. |
Parse |
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. |
Parse |
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. |
Parse |
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. |
Subtract(Time |
Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance. |
To |
Converts the value of the current TimeSpan object to its equivalent string representation. |
To |
Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information. |
To |
Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format. |
Try |
Tries to format the value of the current instance as UTF-8 into the provided span of bytes. |
Try |
Tries to format the value of the current timespan number instance into the provided span of characters. |
Try |
Converts the span 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. |
Try |
Converts the span representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded. |
Try |
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. |
Try |
Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded. |
Try |
Converts the specified span 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. |
Try |
Converts the specified span 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. |
Try |
Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. The format of the string representation must match one of the specified formats exactly. |
Try |
Converts the specified span 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. |
Try |
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. |
Try |
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. |
Try |
Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. The format of the string representation must match one of the specified formats exactly. |
Try |
Converts the specified 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. |
Addition(Time |
Adds two specified TimeSpan instances. |
Division(Time |
Returns a new TimeSpan object whose value is the result of dividing the specified |
Division(Time |
Returns a new Double value that's the result of dividing |
Equality(Time |
Indicates whether two TimeSpan instances are equal. |
Greater |
Indicates whether a specified TimeSpan is greater than another specified TimeSpan. |
Greater |
Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan. |
Inequality(Time |
Indicates whether two TimeSpan instances are not equal. |
Less |
Indicates whether a specified TimeSpan is less than another specified TimeSpan. |
Less |
Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan. |
Multiply(Double, Time |
Returns a new TimeSpan object whose value is the result of multiplying the specified |
Multiply(Time |
Returns a new TimeSpan object whose value is the result of multiplying the specified |
Subtraction(Time |
Subtracts a specified TimeSpan from another specified TimeSpan. |
Unary |
Returns a TimeSpan whose value is the negated value of the specified instance. |
Unary |
Returns the specified instance of TimeSpan. |
IComparable. |
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. |
Product | Versions |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET feedback
.NET is an open source project. Select a link to provide feedback: