TimeSpan.From methods in C# ()


The TimeSpan.From methods include FromDays, FromHours, FromMinutes, etc.

To get the TimeSpan for all the methods

// Days
TimeSpan t1 = TimeSpan.FromDays(1);

// Hours
TimeSpan t2 = TimeSpan.FromHours(1);

// Minutes
TimeSpan t3 = TimeSpan.FromMinutes(1);

The following is the code that works on all the TimeSpan methods −

Example

 Live Demo

using System;
using System.Linq;
public class Demo {
   public static void Main() {
      TimeSpan t1 = TimeSpan.FromDays(1);
      TimeSpan t2 = TimeSpan.FromHours(1);
      TimeSpan t3 = TimeSpan.FromMinutes(1);
      Console.WriteLine(t1);
      Console.WriteLine(t2);
      Console.WriteLine(t3);
   }
}

Output

1.00:00:00
01:00:00
00:01:00

Updated on: 22-Jun-2020

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements