DateTime.AddMinutes(Double) 方法

定义

返回一个新的 DateTime,它将指定的分钟数加到此实例的值上。

public:
 DateTime AddMinutes(double value);
public DateTime AddMinutes (double value);
member this.AddMinutes : double -> DateTime
Public Function AddMinutes (value As Double) As DateTime

参数

value
Double

由整数和小数部分组成的分钟数。 value 参数可以是负数也可以是正数。

返回

一个对象,其值是此实例所表示的日期和时间与 value 所表示的分钟数之和。

例外

示例

以下示例使用 AddMinutes 方法将一些整数和小数值添加到日期和时间。

using namespace System;

void main()
{
   DateTime dateValue(2013, 9, 15, 12, 0, 0);
   
   array<Double>^ minutes = { .01667, .08333, .16667, .25, .33333, 
                              .5, .66667, 1, 2, 15, 30, 17, 45, 
                              60, 180, 60 * 24 };
   
   for each (Double minute in minutes)
      Console::WriteLine("{0} + {1} minute(s) = {2}", dateValue, minute, 
                         dateValue.AddMinutes(minute));
}
// The example displays the following output on a system whose current culture is en-US:
//    9/15/2013 12:00:00 PM + 0.01667 minute(s) = 9/15/2013 12:00:01 PM
//    9/15/2013 12:00:00 PM + 0.08333 minute(s) = 9/15/2013 12:00:05 PM
//    9/15/2013 12:00:00 PM + 0.16667 minute(s) = 9/15/2013 12:00:10 PM
//    9/15/2013 12:00:00 PM + 0.25 minute(s) = 9/15/2013 12:00:15 PM
//    9/15/2013 12:00:00 PM + 0.33333 minute(s) = 9/15/2013 12:00:20 PM
//    9/15/2013 12:00:00 PM + 0.5 minute(s) = 9/15/2013 12:00:30 PM
//    9/15/2013 12:00:00 PM + 0.66667 minute(s) = 9/15/2013 12:00:40 PM
//    9/15/2013 12:00:00 PM + 1 minute(s) = 9/15/2013 12:01:00 PM
//    9/15/2013 12:00:00 PM + 2 minute(s) = 9/15/2013 12:02:00 PM
//    9/15/2013 12:00:00 PM + 15 minute(s) = 9/15/2013 12:15:00 PM
//    9/15/2013 12:00:00 PM + 30 minute(s) = 9/15/2013 12:30:00 PM
//    9/15/2013 12:00:00 PM + 17 minute(s) = 9/15/2013 12:17:00 PM
//    9/15/2013 12:00:00 PM + 45 minute(s) = 9/15/2013 12:45:00 PM
//    9/15/2013 12:00:00 PM + 60 minute(s) = 9/15/2013 1:00:00 PM
//    9/15/2013 12:00:00 PM + 180 minute(s) = 9/15/2013 3:00:00 PM
//    9/15/2013 12:00:00 PM + 1440 minute(s) = 9/16/2013 12:00:00 PM
using System;

public class Example
{
   public static void Main()
   {
      DateTime dateValue = new DateTime(2013, 9, 15, 12, 0, 0);

      double[] minutes = { .01667, .08333, .16667, .25, .33333,
                           .5, .66667, 1, 2, 15, 30, 17, 45,
                           60, 180, 60 * 24 };

      foreach (double minute in minutes)
         Console.WriteLine("{0} + {1} minute(s) = {2}", dateValue, minute,
                           dateValue.AddMinutes(minute));
   }
}
// The example displays the following output on a system whose current culture is en-US:
//    9/15/2013 12:00:00 PM + 0.01667 minute(s) = 9/15/2013 12:00:01 PM
//    9/15/2013 12:00:00 PM + 0.08333 minute(s) = 9/15/2013 12:00:05 PM
//    9/15/2013 12:00:00 PM + 0.16667 minute(s) = 9/15/2013 12:00:10 PM
//    9/15/2013 12:00:00 PM + 0.25 minute(s) = 9/15/2013 12:00:15 PM
//    9/15/2013 12:00:00 PM + 0.33333 minute(s) = 9/15/2013 12:00:20 PM
//    9/15/2013 12:00:00 PM + 0.5 minute(s) = 9/15/2013 12:00:30 PM
//    9/15/2013 12:00:00 PM + 0.66667 minute(s) = 9/15/2013 12:00:40 PM
//    9/15/2013 12:00:00 PM + 1 minute(s) = 9/15/2013 12:01:00 PM
//    9/15/2013 12:00:00 PM + 2 minute(s) = 9/15/2013 12:02:00 PM
//    9/15/2013 12:00:00 PM + 15 minute(s) = 9/15/2013 12:15:00 PM
//    9/15/2013 12:00:00 PM + 30 minute(s) = 9/15/2013 12:30:00 PM
//    9/15/2013 12:00:00 PM + 17 minute(s) = 9/15/2013 12:17:00 PM
//    9/15/2013 12:00:00 PM + 45 minute(s) = 9/15/2013 12:45:00 PM
//    9/15/2013 12:00:00 PM + 60 minute(s) = 9/15/2013 1:00:00 PM
//    9/15/2013 12:00:00 PM + 180 minute(s) = 9/15/2013 3:00:00 PM
//    9/15/2013 12:00:00 PM + 1440 minute(s) = 9/16/2013 12:00:00 PM
open System

let dateValue = DateTime(2013, 9, 15, 12, 0, 0)

let minutes =
    [ 0.01667; 0.08333; 0.16667; 0.25; 0.33333
      0.5; 0.66667; 1; 2; 15; 30; 17; 45
      60; 180; 60. * 24. ]

for minute in minutes do
    printfn $"{dateValue} + {minute} minute(s) = {dateValue.AddMinutes minute}"
                    

// The example displays the following output on a system whose current culture is en-US:
//    9/15/2013 12:00:00 PM + 0.01667 minute(s) = 9/15/2013 12:00:01 PM
//    9/15/2013 12:00:00 PM + 0.08333 minute(s) = 9/15/2013 12:00:05 PM
//    9/15/2013 12:00:00 PM + 0.16667 minute(s) = 9/15/2013 12:00:10 PM
//    9/15/2013 12:00:00 PM + 0.25 minute(s) = 9/15/2013 12:00:15 PM
//    9/15/2013 12:00:00 PM + 0.33333 minute(s) = 9/15/2013 12:00:20 PM
//    9/15/2013 12:00:00 PM + 0.5 minute(s) = 9/15/2013 12:00:30 PM
//    9/15/2013 12:00:00 PM + 0.66667 minute(s) = 9/15/2013 12:00:40 PM
//    9/15/2013 12:00:00 PM + 1 minute(s) = 9/15/2013 12:01:00 PM
//    9/15/2013 12:00:00 PM + 2 minute(s) = 9/15/2013 12:02:00 PM
//    9/15/2013 12:00:00 PM + 15 minute(s) = 9/15/2013 12:15:00 PM
//    9/15/2013 12:00:00 PM + 30 minute(s) = 9/15/2013 12:30:00 PM
//    9/15/2013 12:00:00 PM + 17 minute(s) = 9/15/2013 12:17:00 PM
//    9/15/2013 12:00:00 PM + 45 minute(s) = 9/15/2013 12:45:00 PM
//    9/15/2013 12:00:00 PM + 60 minute(s) = 9/15/2013 1:00:00 PM
//    9/15/2013 12:00:00 PM + 180 minute(s) = 9/15/2013 3:00:00 PM
//    9/15/2013 12:00:00 PM + 1440 minute(s) = 9/16/2013 12:00:00 PM
Module Example
   Public Sub Main()
      Dim minutes() As Double = {.01667, .08333, .16667, .25, .33333, 
                                 .5, .66667, 1, 2, 15, 30, 17, 45, 
                                 60, 180, 60 * 24 }
      Dim dateValue As Date = #9/15/2013 12:00#
      
      For Each minute As Double In minutes
         Console.WriteLine("{0} + {1} minute(s) = {2}", dateValue, 
                           minute, dateValue.AddMinutes(minute))
      Next                             
   End Sub
End Module
' The example displays the following output on a system whose current culture is en-US:
'    9/15/2013 12:00:00 PM + 0.01667 minute(s) = 9/15/2013 12:00:01 PM
'    9/15/2013 12:00:00 PM + 0.08333 minute(s) = 9/15/2013 12:00:05 PM
'    9/15/2013 12:00:00 PM + 0.16667 minute(s) = 9/15/2013 12:00:10 PM
'    9/15/2013 12:00:00 PM + 0.25 minute(s) = 9/15/2013 12:00:15 PM
'    9/15/2013 12:00:00 PM + 0.33333 minute(s) = 9/15/2013 12:00:20 PM
'    9/15/2013 12:00:00 PM + 0.5 minute(s) = 9/15/2013 12:00:30 PM
'    9/15/2013 12:00:00 PM + 0.66667 minute(s) = 9/15/2013 12:00:40 PM
'    9/15/2013 12:00:00 PM + 1 minute(s) = 9/15/2013 12:01:00 PM
'    9/15/2013 12:00:00 PM + 2 minute(s) = 9/15/2013 12:02:00 PM
'    9/15/2013 12:00:00 PM + 15 minute(s) = 9/15/2013 12:15:00 PM
'    9/15/2013 12:00:00 PM + 30 minute(s) = 9/15/2013 12:30:00 PM
'    9/15/2013 12:00:00 PM + 17 minute(s) = 9/15/2013 12:17:00 PM
'    9/15/2013 12:00:00 PM + 45 minute(s) = 9/15/2013 12:45:00 PM
'    9/15/2013 12:00:00 PM + 60 minute(s) = 9/15/2013 1:00:00 PM
'    9/15/2013 12:00:00 PM + 180 minute(s) = 9/15/2013 3:00:00 PM
'    9/15/2013 12:00:00 PM + 1440 minute(s) = 9/16/2013 12:00:00 PM

注解

此方法不会更改此 DateTime的值。 相反,它返回一个新的 DateTime ,其值是此操作的结果。

的小数部分 value 是一分钟的小数部分。 例如,4.5 相当于 4 分钟、30 秒、0 毫秒和 0 个刻度。

在 .NET 6 及更早版本中, value 参数四舍五入为最接近的毫秒。 在 .NET 7 及更高版本中,将使用 参数的完整Doublevalue精度。 但是,由于浮点数学固有的不精确性,生成的精度将有所不同。

适用于

另请参阅