TimeSpan
Struct · namespace System · Since 0.19
struct TimeSpan : IComparable<TimeSpan>, IEquatable<TimeSpan>, IHashableA duration (or a time of day): a signed number of ticks of 100 ns, like .NET’s TimeSpan (with methods instead of
properties).
TimeSpan t = TimeSpan.Create(1, 30, 0); // 01:30:00Console.WriteLine(t.TotalMinutes()); // 90TimeSpan left = deadline.Subtract(DateTime.Now());Fields
Section titled “Fields”| Name | Description |
|---|---|
Ticks |
The duration in ticks of 100 ns. |
int64 TicksThe duration in ticks of 100 ns.
Static methods
Section titled “Static methods”| Name | Description |
|---|---|
FromTicks |
A duration of ticks ticks (100 ns each). |
FromDays |
A duration of days days (rounded to whole ticks). |
FromHours |
A duration of hours hours (rounded to whole ticks). |
FromMinutes |
A duration of minutes minutes (rounded to whole ticks). |
FromSeconds |
A duration of seconds seconds (rounded to whole ticks). |
FromMilliseconds |
A duration of milliseconds milliseconds (rounded to whole ticks). |
Create |
A duration of the given hours, minutes and seconds. |
FromTicks
Section titled “FromTicks”static TimeSpan FromTicks(int64 ticks)A duration of ticks ticks (100 ns each).
FromDays
Section titled “FromDays”static TimeSpan FromDays(double days)A duration of days days (rounded to whole ticks).
FromHours
Section titled “FromHours”static TimeSpan FromHours(double hours)A duration of hours hours (rounded to whole ticks).
FromMinutes
Section titled “FromMinutes”static TimeSpan FromMinutes(double minutes)A duration of minutes minutes (rounded to whole ticks).
FromSeconds
Section titled “FromSeconds”static TimeSpan FromSeconds(double seconds)A duration of seconds seconds (rounded to whole ticks).
FromMilliseconds
Section titled “FromMilliseconds”static TimeSpan FromMilliseconds(double milliseconds)A duration of milliseconds milliseconds (rounded to whole ticks).
Create
Section titled “Create”static TimeSpan Create(int hours, int minutes, int seconds)A duration of the given hours, minutes and seconds.
static TimeSpan Create(int days, int hours, int minutes, int seconds)A duration of the given days, hours, minutes and seconds.
static TimeSpan Create(int days, int hours, int minutes, int seconds, int milliseconds)A duration of the given days, hours, minutes, seconds and milliseconds.
Methods
Section titled “Methods”| Name | Description |
|---|---|
Days |
The days part of the duration. |
Hours |
The hours part of the duration (-23 to 23). |
Minutes |
The minutes part of the duration (-59 to 59). |
Seconds |
The seconds part of the duration (-59 to 59). |
Milliseconds |
The milliseconds part of the duration (-999 to 999). |
TotalDays |
The whole duration in days, with fractions. |
TotalHours |
The whole duration in hours, with fractions. |
TotalMinutes |
The whole duration in minutes, with fractions. |
TotalSeconds |
The whole duration in seconds, with fractions. |
TotalMilliseconds |
The whole duration in milliseconds, with fractions. |
Add |
The sum of this duration and other. |
Subtract |
This duration minus other. |
Negate |
The duration with the opposite sign. |
Duration |
The absolute value of the duration. |
Multiply |
The duration times factor (rounded to whole ticks). |
CompareTo |
Compares the lengths of the durations (IComparable). |
Equals |
Whether the durations are equal (IEquatable). |
GetHashCode |
A hash code of the duration (IHashable). |
ToString |
The duration as text, like .NET’s "c" format: [-][d.]hh:mm:ss[.fffffff], e.g. 1.02:03:04.5000000. |
int Days()The days part of the duration. The parts have the sign of the whole: -1.02:03:04 has the days -1, the hours -2, …
int Hours()The hours part of the duration (-23 to 23).
Minutes
Section titled “Minutes”int Minutes()The minutes part of the duration (-59 to 59).
Seconds
Section titled “Seconds”int Seconds()The seconds part of the duration (-59 to 59).
Milliseconds
Section titled “Milliseconds”int Milliseconds()The milliseconds part of the duration (-999 to 999).
TotalDays
Section titled “TotalDays”double TotalDays()The whole duration in days, with fractions.
TotalHours
Section titled “TotalHours”double TotalHours()The whole duration in hours, with fractions.
TotalMinutes
Section titled “TotalMinutes”double TotalMinutes()The whole duration in minutes, with fractions.
TotalSeconds
Section titled “TotalSeconds”double TotalSeconds()The whole duration in seconds, with fractions.
TotalMilliseconds
Section titled “TotalMilliseconds”double TotalMilliseconds()The whole duration in milliseconds, with fractions.
TimeSpan Add(TimeSpan other)The sum of this duration and other.
Subtract
Section titled “Subtract”TimeSpan Subtract(TimeSpan other)This duration minus other.
Negate
Section titled “Negate”TimeSpan Negate()The duration with the opposite sign.
Duration
Section titled “Duration”TimeSpan Duration()The absolute value of the duration.
Multiply
Section titled “Multiply”TimeSpan Multiply(double factor)The duration times factor (rounded to whole ticks).
CompareTo
Section titled “CompareTo”int CompareTo(TimeSpan other)Compares the lengths of the durations (IComparable).
Equals
Section titled “Equals”bool Equals(TimeSpan other)Whether the durations are equal (IEquatable).
GetHashCode
Section titled “GetHashCode”int GetHashCode()A hash code of the duration (IHashable).
ToString
Section titled “ToString”string ToString()The duration as text, like .NET’s "c" format: [-][d.]hh:mm:ss[.fffffff], e.g. 1.02:03:04.5000000.