TimeSpan Constructor

In this chapter you will learn:

  1. Initialize a time span to 1:02:03
  2. Initialize a time span to 250 milliseconds
  3. Initialize a time span to 14 days

Initialize a time span to 1:02:03

using System;//from j  a  v  a  2 s . c o m

public class ToString
{
   public static void Main()
   {
      TimeSpan span;

      // Initialize a time span to 1:02:03.
      span = new TimeSpan(1, 2, 3);

   }

}

Initialize a time span to 250 milliseconds

using System;//from   ja  va2 s  . c o m

public class ToString
{
   public static void Main()
   {
      TimeSpan span;

      // Initialize a time span to 250 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 250);

   }

}

Initialize a time span to 14 days

using System;/*from j a  v  a 2 s.co  m*/

public class ToString
{
   public static void Main()
   {
      TimeSpan span;

      // Initialize a time span to 14 days.
      span = new TimeSpan(-14, 0, 0, 0, 0);

   }

}

The following code creates a TimeSpan representing 2.5 days.

TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0); Console.WriteLine(timespan1);

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to get the duration of a TimeSpan
Home » C# Tutorial » Date, Time, TimeZone
Compare DateTimeOffset
DateTimeOffset creation
Date time Value from DateTimeOffset
Compare DateTime value
DateTime constant value
DateTime constructor
DateTime properties
Create TimeSpan From
TimeSpan
TimeSpan add and subtract
DateTime and TimeSpan
Compare TimeSpan
TimeSpan constant
TimeSpan Constructor
TimeSpan Duration
TimeSpan format
Negate a TimeSpan
TimeSpan operators
Parse string to TimeSpan
TimeSpan to string
TimeSpan total properties
TimeSpan propeties
Daylight saving
Time zone ID
UTC offset with TimeZone
TimeZone converting
TimeZone creation