DateTimeOffset.AddDays(Double) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
返回一个新的 DateTimeOffset 对象,它将由整数和小数部分组成的指定天数加到此实例的值上。
public:
DateTimeOffset AddDays(double days);
public DateTimeOffset AddDays (double days);
member this.AddDays : double -> DateTimeOffset
Public Function AddDays (days As Double) As DateTimeOffset
参数
- days
- Double
由整数和小数部分组成的天数。 此数值可以是负数也可以是正数。
返回
一个对象,其值为当前的 DateTimeOffset 对象所表示的日期和时间与 days
所表示的天数之和。
例外
示例
以下示例使用 AddDays 方法列出 2008 年 3 月星期一(工作周的开始时间)的日期。
DateTimeOffset workDay = new DateTimeOffset(2008, 3, 1, 9, 0, 0,
DateTimeOffset.Now.Offset);
int month = workDay.Month;
// Start with the first Monday of the month
if (workDay.DayOfWeek != DayOfWeek.Monday)
{
if (workDay.DayOfWeek == DayOfWeek.Sunday)
workDay = workDay.AddDays(1);
else
workDay = workDay.AddDays(8 - (int)workDay.DayOfWeek);
}
Console.WriteLine("Beginning of Work Week In {0:MMMM} {0:yyyy}:", workDay);
// Add one week to the current date
do
{
Console.WriteLine(" {0:dddd}, {0:MMMM}{0: d}", workDay);
workDay = workDay.AddDays(7);
} while (workDay.Month == month);
// The example produces the following output:
// Beginning of Work Week In March 2008:
// Monday, March 3
// Monday, March 10
// Monday, March 17
// Monday, March 24
// Monday, March 31
let workDay = DateTimeOffset(2008, 3, 1, 9, 0, 0, DateTimeOffset.Now.Offset)
let month = workDay.Month
// Start with the first Monday of the month
let mutable workDay =
match workDay.DayOfWeek with
| DayOfWeek.Monday ->
workDay
| DayOfWeek.Sunday ->
workDay.AddDays 1
| _ ->
workDay.AddDays(8. - float workDay.DayOfWeek)
printfn $"Beginning of Work Week In {workDay:MMMM} {workDay:yyyy}:"
// Add one week to the current date
while workDay.Month = month do
printfn $" {workDay:dddd}, {workDay:MMMM}{workDay: d}"
workDay <- workDay.AddDays 7
// The example produces the following output:
// Beginning of Work Week In March 2008:
// Monday, March 3
// Monday, March 10
// Monday, March 17
// Monday, March 24
// Monday, March 31
Dim workDay As New DateTimeOffset(#3/1/2008 9:00AM#, _
DateTimeOffset.Now.Offset)
Dim month As Integer = workDay.Month
' Start with the first Monday of the month
If workDay.DayOfWeek <> DayOfWeek.Monday Then
If workDay.DayOfWeek = DayOfWeek.Sunday Then
workDay = workDay.AddDays(1)
Else
workDay = workDay.AddDays(8 - CInt(workDay.DayOfWeek))
End If
End If
Console.WriteLine("Beginning of Work Week In {0:MMMM} {0:yyyy}:", workDay)
' Add one week to the current date
Do While workDay.Month = month
Console.WriteLine(" {0:dddd}, {0:MMMM}{0: d}", workDay)
workDay = workDay.AddDays(7)
Loop
' The example produces the following output:
' Beginning of Work Week In March 2008:
' Monday, March 3
' Monday, March 10
' Monday, March 17
' Monday, March 24
' Monday, March 31
注解
参数的小 days
数部分是一天的小数部分。 例如,4.5 相当于 4 天 12 小时 0 分钟 0 秒 0 毫秒。
在 .NET 6 及更早版本中, days
参数四舍五入为最接近的毫秒。 在 .NET 7 及更高版本中,将使用 参数的完整Doubledays
精度。 但是,由于浮点数学固有的不精确性,生成的精度将有所不同。
注意
此方法返回一个新 DateTimeOffset 对象。 它不会通过将添加到 days
当前对象的日期和时间来修改其值。
DateTimeOffset由于 对象不表示特定时区中的日期和时间,因此该方法AddDays在执行日期和时间算术时不考虑特定时区的调整规则。
将小于一天的时间间隔转换为分数可能会造成精度损失。 如果这是有问题的,可以使用 Add 方法,该方法使你能够在单个方法调用中指定多种类型的时间间隔,并且无需将时间间隔转换为一天的小数部分。