TimeSpan format

In this chapter you will learn:

  1. Custom TimeSpan Format Strings

Custom TimeSpan Format Strings

using System;/*from  j  ava2s  .  co m*/

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = duration.ToString("%d") + " days";
      Console.WriteLine(output);

      output = duration.ToString(@"dd\.hh\:mm\:ss"); 
      Console.WriteLine(output);

      Console.WriteLine("{0:%d} day(s)", duration);
      Console.WriteLine("{0:dd\\.hh\\:mm\\:ss} days", duration);
   }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to negate a TimeSpan