Table of Contents

Struct TimeSpan

Namespace
System
Assembly
mscorlib.dll

Represents a time interval.

public struct TimeSpan
Inherited Members

Remarks

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 structure should be used instead. 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 MinValue to MaxValue.

Constructors

TimeSpan(int, int, int)

Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds.

public TimeSpan(int hours, int minutes, int seconds)

Parameters

hours int

Number of hours.

minutes int

Number of minutes.

seconds int

Number of seconds.

TimeSpan(int, int, int, int)

Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, and seconds.

public TimeSpan(int days, int hours, int minutes, int seconds)

Parameters

days int

Number of days.

hours int

Number of hours.

minutes int

Number of minutes.

seconds int

Number of seconds.

TimeSpan(int, int, int, int, int)

Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds and milliseconds.

public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds)

Parameters

days int

Number of days.

hours int

Number of hours.

minutes int

Number of minutes.

seconds int

Number of seconds.

milliseconds int

Number of milliseconds.

TimeSpan(long)

Initializes a new instance of the TimeSpan structure to the specified number of ticks.

public TimeSpan(long ticks)

Parameters

ticks long

A time period expressed in 100-nanosecond units.

Fields

MaxValue

Represents the maximum TimeSpan value. This field is read-only.

public static readonly TimeSpan MaxValue

Field Value

TimeSpan

MinValue

Represents the minimum TimeSpan value. This field is read-only.

public static readonly TimeSpan MinValue

Field Value

TimeSpan

TicksPerDay

Represents the number of ticks in 1 day. This field is constant.

public const long TicksPerDay = 864000000000

Field Value

long

TicksPerHour

Represents the number of ticks in 1 hour. This field is constant.

public const long TicksPerHour = 36000000000

Field Value

long

TicksPerMillisecond

Represents the number of ticks in 1 millisecond. This field is constant.

public const long TicksPerMillisecond = 10000

Field Value

long

TicksPerMinute

Represents the number of ticks in 1 minute. This field is constant.

public const long TicksPerMinute = 600000000

Field Value

long

TicksPerSecond

Represents the number of ticks in 1 second.

public const long TicksPerSecond = 10000000

Field Value

long

Zero

Represents the zero TimeSpan value. This field is read-only.

public static readonly TimeSpan Zero

Field Value

TimeSpan

Properties

Days

Gets the days component of the time interval represented by the current TimeSpan structure.

public int Days { get; }

Property Value

int

The day component of this instance. The return value can be positive or negative.

Hours

Gets the hours component of the time interval represented by the current TimeSpan structure.

public int Hours { get; }

Property Value

int

The hour component of this instance. The return value ranges from -23 through 23.

Milliseconds

Gets the milliseconds component of the time interval represented by the current TimeSpan structure.

public int Milliseconds { get; }

Property Value

int

The millisecond component of this instance. The return value ranges from -999 through 999.

Minutes

Gets the minutes component of the time interval represented by the current TimeSpan structure.

public int Minutes { get; }

Property Value

int

The minute component of this instance. The return value ranges from -59 through 59.

Seconds

Gets the seconds component of the time interval represented by the current TimeSpan structure.

public int Seconds { get; }

Property Value

int

The second component of this instance. The return value ranges from -59 through 59.

Ticks

Gets the number of ticks that represent the value of the current TimeSpan structure.

public long Ticks { get; }

Property Value

long

The number of ticks contained in this instance.

TotalDays

Gets the value of the current TimeSpan structure expressed in whole and fractional days.

public double TotalDays { get; }

Property Value

double

The total number of days represented by this instance.

TotalHours

Gets the value of the current TimeSpan structure expressed in whole and fractional hours.

public double TotalHours { get; }

Property Value

double

The total number of hours represented by this instance.

TotalMilliseconds

Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds.

public double TotalMilliseconds { get; }

Property Value

double

The total number of milliseconds represented by this instance.

TotalMinutes

Gets the value of the current TimeSpan structure expressed in whole and fractional minutes.

public double TotalMinutes { get; }

Property Value

double

The total number of minutes represented by this instance.

TotalSeconds

Gets the value of the current TimeSpan structure expressed in whole and fractional seconds.

public double TotalSeconds { get; }

Property Value

double

The total number of seconds represented by this instance.

Methods

Add(TimeSpan)

Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance.

public TimeSpan Add(TimeSpan ts)

Parameters

ts TimeSpan

The time interval to add.

Returns

TimeSpan

A new object that represents the value of this instance plus the value of ts.

Compare(TimeSpan, TimeSpan)

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.

public static int Compare(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

int

One of the following values :

-1 if t1 is shorter than t2.

0 if t1 is equal to t2.

1 if t1 is longer than t2.

CompareTo(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 int CompareTo(object value)

Parameters

value object

An object to compare, or null.

Returns

int

One of the following values :

-1 if This instance is shorter than value.

0 if This instance is equal to value.

1 if This instance is longer than value or value is null.

Duration()

Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.

public TimeSpan Duration()

Returns

TimeSpan

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

public override bool Equals(object value)

Parameters

value object

An object to compare with this instance.

Returns

bool

true if value is a TimeSpan object that represents the same time interval as the current TimeSpan structure; otherwise, false.

Equals(TimeSpan, TimeSpan)

Returns a value that indicates whether two specified instances of TimeSpan are equal.

public static bool Equals(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the values of t1 and t2 are equal; otherwise, false.

FromDays(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of days.

public static TimeSpan FromDays(long value)

Parameters

value long

A number of days that represent a time.

Returns

TimeSpan

An object that represents value.

FromHours(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of hours.

public static TimeSpan FromHours(long value)

Parameters

value long

A number of hours that represent a time.

Returns

TimeSpan

An object that represents value.

FromMilliseconds(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of milliseconds.

public static TimeSpan FromMilliseconds(long value)

Parameters

value long

A number of milliseconds that represent a time.

Returns

TimeSpan

An object that represents value.

FromMinutes(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of minute.

public static TimeSpan FromMinutes(long value)

Parameters

value long

A number of minute that represent a time.

Returns

TimeSpan

An object that represents value.

FromSeconds(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of seconds.

public static TimeSpan FromSeconds(long value)

Parameters

value long

A number of seconds that represent a time.

Returns

TimeSpan

An object that represents value.

FromTicks(long)

Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.

public static TimeSpan FromTicks(long value)

Parameters

value long

A number of ticks that represent a time.

Returns

TimeSpan

An object that represents value.

GetHashCode()

Returns the hash code for this instance.

public override int GetHashCode()

Returns

int

A 32-bit signed integer hash code.

Negate()

Returns a new TimeSpan object whose value is the negated value of this instance.

public TimeSpan Negate()

Returns

TimeSpan

A new object with the same numeric value as this instance, but with the opposite sign.

Subtract(TimeSpan)

Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance.

public TimeSpan Subtract(TimeSpan ts)

Parameters

ts TimeSpan

The time interval to be subtracted.

Returns

TimeSpan

A new time interval whose value is the result of the value of this instance minus the value of ts.

ToString()

Converts the value of the current TimeSpan object to its equivalent string representation.

public override string ToString()

Returns

string

The string representation of the current TimeSpan value.

Remarks

The returned string is formatted with the "c" format specifier and has the following format: [-][d.]hh:mm:ss[.fffffff]

Operators

operator +(TimeSpan, TimeSpan)

Adds two specified TimeSpan instances.

public static TimeSpan operator +(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to add.

t2 TimeSpan

The second time interval to add.

Returns

TimeSpan

An object whose value is the sum of the values of t1 and t2.

operator ==(TimeSpan, TimeSpan)

Indicates whether two TimeSpan instances are equal

public static bool operator ==(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the values of t1 and t2 are equal; otherwise, false.

operator >(TimeSpan, TimeSpan)

Indicates whether a specified TimeSpan is greater than another specified TimeSpan.

public static bool operator >(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the value of t1 is greater than the value of t2; otherwise, false.

operator >=(TimeSpan, TimeSpan)

Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan.

public static bool operator >=(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the value of t1 is greater than or equal to the value of t2; otherwise, false.

operator !=(TimeSpan, TimeSpan)

Indicates whether two TimeSpan instances are not equal.

public static bool operator !=(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the values of t1 and t2 are not equal; otherwise, false.

operator <(TimeSpan, TimeSpan)

Indicates whether a specified TimeSpan is less than another specified TimeSpan.

public static bool operator <(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the value of t1 is less than the value of t2; otherwise, false.

operator <=(TimeSpan, TimeSpan)

Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan.

public static bool operator <=(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The first time interval to compare.

t2 TimeSpan

The second time interval to compare.

Returns

bool

true if the value of t1 is less than or equal to the value of t2; otherwise, false.

operator -(TimeSpan, TimeSpan)

Subtracts a specified TimeSpan from another specified TimeSpan.

public static TimeSpan operator -(TimeSpan t1, TimeSpan t2)

Parameters

t1 TimeSpan

The minuend.

t2 TimeSpan

The subtrahend.

Returns

TimeSpan

An object whose value is the result of the value of t1 minus the value of t2.

operator -(TimeSpan)

Returns a TimeSpan whose value is the negated value of the specified instance.

public static TimeSpan operator -(TimeSpan t)

Parameters

t TimeSpan

The time interval to be negated.

Returns

TimeSpan

An object that has the same numeric value as this instance, but the opposite sign.

operator +(TimeSpan)

Returns the specified instance of TimeSpan.

public static TimeSpan operator +(TimeSpan t)

Parameters

t TimeSpan

The time interval to return.

Returns

TimeSpan

The time interval specified by t.