System.TimeSpan Structure

Namespace: System

Represents a time interval.

Syntax

public struct TimeSpan
{
    // Constructors
    public TimeSpan(long ticks);
    public TimeSpan(int hours, int minutes, int seconds);
    public TimeSpan(int days, int hours, int minutes, int seconds);
    public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);

    // Properties
    public static TimeSpan MaxValue { get; }
    public static TimeSpan MinValue { get; }
    public static TimeSpan Zero { get; }
    public int Days { get; }
    public int Hours { get; }
    public int Minutes { get; }
    public int Seconds { get; }
    public int Milliseconds { get; }
    public long Ticks { get; }
    public double TotalDays { get; }
    public double TotalHours { get; }
    public double TotalMinutes { get; }
    public double TotalSeconds { get; }
    public double TotalMilliseconds { get; }

    // Methods
    public static TimeSpan FromDays(double value);
    public static TimeSpan FromHours(double value);
    public static TimeSpan FromMinutes(double value);
    public static TimeSpan FromSeconds(double value);
    public static TimeSpan FromMilliseconds(double value);
    public static TimeSpan FromTicks(long value);
    public TimeSpan Add(TimeSpan ts);
    public TimeSpan Subtract(TimeSpan ts);
    public TimeSpan Negate();
    public TimeSpan Duration();
    public int CompareTo(object value);
    public int CompareTo(TimeSpan ts);
    public bool Equals(TimeSpan other);
    public static int Compare(TimeSpan t1, TimeSpan t2);
    public static bool Equals(TimeSpan t1, TimeSpan t2);
    public static TimeSpan operator +(TimeSpan t1, TimeSpan t2);
    public static TimeSpan operator -(TimeSpan t1, TimeSpan t2);
    public static TimeSpan operator -(TimeSpan t);
    public static bool operator ==(TimeSpan t1, TimeSpan t2);
    public static bool operator !=(TimeSpan t1, TimeSpan t2);
    public static bool operator <(TimeSpan t1, TimeSpan t2);
    public static bool operator >(TimeSpan t1, TimeSpan t2);
    public static bool operator <=(TimeSpan t1, TimeSpan t2);
    public static bool operator >=(TimeSpan t1, TimeSpan t2);
    public static TimeSpan Parse(string s);
    public static bool TryParse(string s, out TimeSpan result);
    public string ToString(string format);
    public override string ToString();
}

Properties

NameTypeDescription
DaysintGets the days component of the time interval.
HoursintGets the hours component, excluding days.
MinutesintGets the minutes component, excluding hours and days.
SecondsintGets the seconds component, excluding minutes, hours, and days.
MillisecondsintGets the milliseconds component, excluding seconds, minutes, hours, and days.
TickslongGets the total number of ticks that represent the value of this instance.
TotalDaysdoubleGets the value of the current TimeSpan expressed in whole and fractional days.
TotalHoursdoubleGets the value of the current TimeSpan expressed in whole and fractional hours.
TotalMinutesdoubleGets the value of the current TimeSpan expressed in whole and fractional minutes.
TotalSecondsdoubleGets the value of the current TimeSpan expressed in whole and fractional seconds.
TotalMillisecondsdoubleGets the value of the current TimeSpan expressed in whole and fractional milliseconds.
ZeroTimeSpanGets a time interval of zero.
MaxValueTimeSpanGets the maximum possible value of TimeSpan.
MinValueTimeSpanGets the minimum possible value of TimeSpan.

Methods

SignatureDescription
static TimeSpan FromDays(double value)Creates a TimeSpan from a number of days.
static TimeSpan FromHours(double value)Creates a TimeSpan from a number of hours.
static TimeSpan FromMinutes(double value)Creates a TimeSpan from a number of minutes.
static TimeSpan FromSeconds(double value)Creates a TimeSpan from a number of seconds.
static TimeSpan FromMilliseconds(double value)Creates a TimeSpan from a number of milliseconds.
static TimeSpan FromTicks(long value)Creates a TimeSpan from a number of ticks.
TimeSpan Add(TimeSpan ts)Adds a specified time interval to this instance.
TimeSpan Subtract(TimeSpan ts)Subtracts a specified time interval from this instance.
TimeSpan Negate()Returns a new TimeSpan object whose value is the negative of this instance.
TimeSpan Duration()Returns a new TimeSpan object whose value is the absolute value of this instance.
static TimeSpan Parse(string s)Converts the string representation of a time interval to its TimeSpan equivalent.
static bool TryParse(string s, out TimeSpan result)Tries to convert the string representation of a time interval to its TimeSpan equivalent.
string ToString(string format)Converts the value of the current instance to its equivalent string representation using the specified format.

Example

The following example demonstrates creating, manipulating, and formatting TimeSpan values.

using System; class Program { static void Main() { // Create a TimeSpan of 2 days, 3 hours, 15 minutes, 30 seconds TimeSpan ts = new TimeSpan(2, 3, 15, 30); Console.WriteLine($"Original: {ts}"); // Add 12 hours TimeSpan plus = ts.Add(TimeSpan.FromHours(12)); Console.WriteLine($"After adding 12 hours: {plus}"); // Subtract 45 minutes TimeSpan minus = plus.Subtract(TimeSpan.FromMinutes(45)); Console.WriteLine($"After subtracting 45 minutes: {minus}"); // Total hours as a double Console.WriteLine($"Total hours: {minus.TotalHours:F2}"); // Parse from string TimeSpan parsed = TimeSpan.Parse("1.02:03:04.005"); Console.WriteLine($"Parsed: {parsed}"); // Format output Console.WriteLine($"Custom format: {parsed:%d} days, {parsed:%h} hours, {parsed:%m} minutes"); } }

See Also