DateTime.AddHours(Double) DateTime.AddHours(Double) DateTime.AddHours(Double) DateTime.AddHours(Double) Method

定义

返回一个新的 DateTime,它将指定的小时数加到此实例的值上。Returns a new DateTime that adds the specified number of hours to the value of this instance.

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

参数

value
Double Double Double Double

由整数和小数部分组成的小时数。A number of whole and fractional hours. value 参数可以是负数也可以是正数。The value parameter can be negative or positive.

返回

一个对象,其值是此实例所表示的日期和时间与 value 所表示的小时数之和。An object whose value is the sum of the date and time represented by this instance and the number of hours represented by value.

异常

示例

下面的示例使用 AddHours 方法将一些整数值和小数部分值添加到日期和时间。The following example uses the AddHours method to add a number of whole and fractional values to a date and time. 它还说明了通过向方法传递包含小数部分的值导致的精度损失。It also illustrates the loss of precision caused by passing the method a value that includes a fractional component.

using System;

public class Example
{
   public static void Main()
   {
      double[] hours = {.08333, .16667, .25, .33333, .5, .66667, 1, 2, 
                        29, 30, 31, 90, 365};
      DateTime dateValue = new DateTime(2009, 3, 1, 12, 0, 0);
      
      foreach (double hour in hours)
         Console.WriteLine("{0} + {1} hour(s) = {2}", dateValue, hour, 
                           dateValue.AddHours(hour));

   }
}
// The example displays the following output on a system whose current
// culture is en-US:
//    3/1/2009 12:00:00 PM + 0.08333 hour(s) = 3/1/2009 12:04:59 PM
//    3/1/2009 12:00:00 PM + 0.16667 hour(s) = 3/1/2009 12:10:00 PM
//    3/1/2009 12:00:00 PM + 0.25 hour(s) = 3/1/2009 12:15:00 PM
//    3/1/2009 12:00:00 PM + 0.33333 hour(s) = 3/1/2009 12:19:59 PM
//    3/1/2009 12:00:00 PM + 0.5 hour(s) = 3/1/2009 12:30:00 PM
//    3/1/2009 12:00:00 PM + 0.66667 hour(s) = 3/1/2009 12:40:00 PM
//    3/1/2009 12:00:00 PM + 1 hour(s) = 3/1/2009 1:00:00 PM
//    3/1/2009 12:00:00 PM + 2 hour(s) = 3/1/2009 2:00:00 PM
//    3/1/2009 12:00:00 PM + 29 hour(s) = 3/2/2009 5:00:00 PM
//    3/1/2009 12:00:00 PM + 30 hour(s) = 3/2/2009 6:00:00 PM
//    3/1/2009 12:00:00 PM + 31 hour(s) = 3/2/2009 7:00:00 PM
//    3/1/2009 12:00:00 PM + 90 hour(s) = 3/5/2009 6:00:00 AM
//    3/1/2009 12:00:00 PM + 365 hour(s) = 3/16/2009 5:00:00 PM
Module Example
   Public Sub Main()
      Dim hours() As Double = {.08333, .16667, .25, .33333, .5, .66667, 1, 2, _
                             29, 30, 31, 90, 365}
      Dim dateValue As Date = #3/1/2009 12:00#
      
      For Each hour As Double In hours
         Console.WriteLine("{0} + {1} hour(s) = {2}", dateValue, hour, _
                           dateValue.AddHours(hour))
      Next                             
   End Sub
End Module
' The example displays the following output on a system whose current
' culture is en-US:
' 3/1/2009 12:00:00 PM + 0.08333 hour(s) = 3/1/2009 12:04:59 PM
' 3/1/2009 12:00:00 PM + 0.16667 hour(s) = 3/1/2009 12:10:00 PM
' 3/1/2009 12:00:00 PM + 0.25 hour(s) = 3/1/2009 12:15:00 PM
' 3/1/2009 12:00:00 PM + 0.33333 hour(s) = 3/1/2009 12:19:59 PM
' 3/1/2009 12:00:00 PM + 0.5 hour(s) = 3/1/2009 12:30:00 PM
' 3/1/2009 12:00:00 PM + 0.66667 hour(s) = 3/1/2009 12:40:00 PM
' 3/1/2009 12:00:00 PM + 1 hour(s) = 3/1/2009 1:00:00 PM
' 3/1/2009 12:00:00 PM + 2 hour(s) = 3/1/2009 2:00:00 PM
' 3/1/2009 12:00:00 PM + 29 hour(s) = 3/2/2009 5:00:00 PM
' 3/1/2009 12:00:00 PM + 30 hour(s) = 3/2/2009 6:00:00 PM
' 3/1/2009 12:00:00 PM + 31 hour(s) = 3/2/2009 7:00:00 PM
' 3/1/2009 12:00:00 PM + 90 hour(s) = 3/5/2009 6:00:00 AM
' 3/1/2009 12:00:00 PM + 365 hour(s) = 3/16/2009 5:00:00 PM

注解

此方法不会更改此 @no__t 的值。This method does not change the value of this DateTime. 相反,它会返回一个新的 DateTime,其值为此操作的结果。Instead, it returns a new DateTime whose value is the result of this operation. 返回的 @no__t 对象的 Kind 属性与 @no__t 的对象相同。The Kind property of the returned DateTime object is the same as that of value.

@No__t 的小数部分是一个小时的小数部分。The fractional part of value is the fractional part of an hour. 例如,4.5 等效于4小时30分钟0秒0毫秒,0刻度。For example, 4.5 is equivalent to 4 hours, 30 minutes, 0 seconds, 0 milliseconds, and 0 ticks.

@No__t 的参数舍入到最接近的毫秒。The value parameter is rounded to the nearest millisecond.

如果结果是非终止的重复小数,则将不到1小时的时间间隔转换为分数可能会损失精度。Converting time intervals of less than an hour to a fraction can involve a loss of precision if the result is a non-terminating repeating decimal. (例如,一分钟为0.016667 小时。)如果这有问题,则可以使用 Add 方法,这使你能够在单个方法调用中指定多个时间间隔,并且无需将时间间隔转换为小时的小数部分。(For example, one minute is 0.016667 of an hour.) If this is problematic, you can use the Add method, which enables you to specify more than one kind of time interval in a single method call and eliminates the need to convert time intervals to fractional parts of an hour.

适用于

另请参阅