Monday, June 28, 2021

C# System.Timespan Class


Timespan class is used to get the difference between two times in C#. Tick is the smallest unit of time in C#.


using System;
namespace ConsoleTests
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt1 = DateTime.Parse("2021/1/2");
            DateTime dt2 = DateTime.Parse("2021/2/3");
            TimeSpan difference = dt2 - dt1;
            Console.WriteLine(difference);
            Console.WriteLine(difference.Days);
            Console.WriteLine(difference.TotalDays);
            Console.WriteLine(difference.TotalHours);
            Console.WriteLine(difference.TotalMinutes);
            Console.WriteLine(difference.TotalSeconds);
            Console.WriteLine(difference.Ticks);
            Console.ReadKey();
        }

    }
}
OUTPUT
32.00:00:00
32
32
768
46080
2764800
27648000000000


Edited on 6th July 2023

No comments:

Post a Comment

Hot Topics