📜  C#中的DateTime.Subtract()方法

📅  最后修改于: 2021-05-29 15:22:03             🧑  作者: Mango

此方法用于从该实例中减去指定的时间或持续时间。此方法的重载列表中有2种方法,如下所示:

  • 减(DateTime)
  • 减(时间跨度)

DateTime.Subtract(DateTime)

此方法用于从该实例中减去指定的日期和时间。

下面的程序说明了DateTime.Subtract(DateTime)方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.Subtract(DateTime)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date1 = new DateTime(2011, 1,
                                     1, 4, 0, 15);
  
            // creating object of DateTime
            DateTime date2 = new DateTime(2010, 1,
                                     1, 4, 0, 15);
  
            // getting ShortTime from DateTime
            // using Subtract() method;
            TimeSpan value = date1.Subtract(date2);
  
            // Display the TimeSpan
            Console.WriteLine("TimeSpan between date1"+
                           " and date2 is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
TimeSpan between date1 and date2 is 365.00:00:00

范例2:

// C# program to demonstrate the
// DateTime.Subtract(DateTime)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date1 = DateTime.MinValue;
  
            // creating object of DateTime
            DateTime date2 = new DateTime(11119999, 1,
                                         1, 4, 0, 15);
  
            // getting ShortTime from DateTime
            // using Subtract() method;
            TimeSpan value = date1.Subtract(date2);
  
            // Display the TimeSpan
            Console.WriteLine("TimeSpan between date1 "+
                             "and date2 is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Exception Thrown: System.ArgumentOutOfRangeException

DateTime.Subtract(TimeSpan)

此方法用于从该实例中减去指定的持续时间。

下面的程序说明了DateTime.Subtract(TimeSpan)方法的用法:

范例1:

// C# program to demonstrate the
// DateTime.Subtract(TimeSpan)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date = new DateTime(2011, 1,
                                    1, 4, 0, 15);
  
            // creating object of TimeSpan
            TimeSpan ts = new TimeSpan(1, 12,
                                     15, 16);
  
            // getting ShortTime from 
            // subtracting DateTime and TimeSpan
            // using Subtract() method;
            DateTime value = date.Subtract(ts);
  
            // Display the TimeSpan
            Console.WriteLine("DateTime between date "+
                               "and ts is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e) 
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
DateTime between date and ts is 12/30/2010 15:44:59

示例2:对于ArgumentOutOfRangeException

// C# program to demonstrate the
// DateTime.Subtract(TimeSpan)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // creating object of DateTime
            DateTime date = DateTime.MinValue;
  
            // creating object of TimeSpan
            TimeSpan ts = new TimeSpan(1, 12, 15, 16);
  
            // getting ShortTime from subtracting 
            // DateTime and TimeSpan
            // using Subtract() method;
            DateTime value = date.Subtract(ts);
  
            // Display the TimeSpan
            Console.WriteLine("DateTime between date"+
                             " and ts is {0}", value);
        }
  
        catch (ArgumentOutOfRangeException e)
        {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}
输出:
Exception Thrown: System.ArgumentOutOfRangeException

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.datetime.subtract?view=netframework-4.7.2