TimeSpan type

To repesent a period of time, such as 2.5 hours we can use TimeSpan.

We can create TimeSpan instances by using its contructors or using its fromXXX methods.

Here are the constructors:


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);
public TimeSpan (long ticks);  // Each tick = 100ns

The static From... methods are more convenient:


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);

using System;
using System.Text;
class Sample
{
    public static void Main()
    {
        Console.WriteLine(new TimeSpan(2, 30, 0));  // 02:30:00
        Console.WriteLine(TimeSpan.FromHours(2.5));  // 02:30:00
        Console.WriteLine(TimeSpan.FromHours(-2.5));  // -02:30:00

    }
}

The output:


02:30:00
02:30:00
-02:30:00
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.