C implements two methods of time subtraction

  • 2020-12-16 06:04:59
  • OfStack

This article gives an example of how C# implements two time subtractions. Share to everybody for everybody reference. Specific implementation methods are as follows:

using System;  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
 
namespace Test 

    class Program 
    { 
        static void Main(string[] args) 
        { 
            DateTime t1 = DateTime.Parse("2007-01-01");  
            DateTime t2 = DateTime.Parse("2006-01-01"); 
 
            System.TimeSpan t3 = t1 - t2;  // Subtract the two times . The default is The number of days between two times    Get: 365.00:00:00 
 
            double getDay = t3.TotalDays; // Convert this number of days to days , The return value is double Type (you don't actually have to cast, because t3 The default is days.) Get:  
 
            double getHours = t3.TotalHours; // Convert this number of days into hours , The return value is double The type of  
 
            double getMinute = t3.TotalMinutes; // Convert this number of days into minutes , The return value is double The type of  
 
            double getSeconds = t3.TotalSeconds; // Convert the number of days to seconds , The return value is double The type of  
 
            double getMillisecond = t3.TotalMilliseconds; //// Convert the number of days to milliseconds , The return value is double The type of  
 
            Console.WriteLine(t3);  // Output: 365.00:00:00 
            Console.WriteLine(getDay); // Output: 365 
            Console.WriteLine(getHours); // Output: 8760 
            Console.WriteLine(getMinute); // Output: 525600 
            Console.WriteLine(getSeconds); // Output: 31536000 
            Console.WriteLine(getMillisecond); // Output: 31536000000 
            Console.ReadKey(); 
        } 
    }
}

Hopefully this article has helped you with your C# programming.


Related articles: