TimeSpan.Add(TimeSpan) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public:
TimeSpan Add(TimeSpan ts);
public TimeSpan Add (TimeSpan ts);
member this.Add : TimeSpan -> TimeSpan
Public Function Add (ts As TimeSpan) As TimeSpan
参数
- ts
- TimeSpan
待添加的时间间隔。
返回
一个新对象,表示此实例的值加 ts
的值。
例外
生成的 TimeSpan 小于 TimeSpan.MinValue 或大于 TimeSpan.MaxValue。
示例
以下示例调用 Add 方法,将时间间隔数组中的每个元素添加到基 TimeSpan 值。
TimeSpan baseTimeSpan = new TimeSpan(1, 12, 15, 16);
// Create an array of timespan intervals.
TimeSpan[] intervals = {
TimeSpan.FromDays(1.5),
TimeSpan.FromHours(1.5),
TimeSpan.FromMinutes(45),
TimeSpan.FromMilliseconds(505),
new TimeSpan(1, 17, 32, 20),
new TimeSpan(-8, 30, 0)
};
// Calculate a new time interval by adding each element to the base interval.
foreach (var interval in intervals)
Console.WriteLine(@"{0,-10:g} {3} {1,15:%d\:hh\:mm\:ss\.ffff} = {2:%d\:hh\:mm\:ss\.ffff}",
baseTimeSpan, interval, baseTimeSpan.Add(interval),
interval < TimeSpan.Zero ? "-" : "+");
// The example displays the following output:
// 1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000
// 1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000
// 1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000
// 1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050
// 1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000
// 1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000
open System
let baseTimeSpan = TimeSpan(1, 12, 15, 16)
// Create a list of timespan intervals.
let intervals =
[ TimeSpan.FromDays 1.5
TimeSpan.FromHours 1.5
TimeSpan.FromMinutes 45
TimeSpan.FromMilliseconds 505
TimeSpan(1, 17, 32, 20)
TimeSpan(-8, 30, 0) ]
// Calculate a new time interval by adding each element to the base interval.
for interval in intervals do
printfn $"""{baseTimeSpan,-10:g} {if interval < TimeSpan.Zero then "-" else "+"} {interval.ToString "%d\:hh\:mm\:ss\.ffff",15} = {baseTimeSpan.Add(interval).ToString "%d\:hh\:mm\:ss\.ffff"}"""
// The example displays the following output:
// 1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000
// 1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000
// 1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000
// 1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050
// 1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000
// 1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000
Module Example
Public Sub Main()
Dim baseTimeSpan As New TimeSpan(1, 12, 15, 16)
' Create an array of timespan intervals.
Dim intervals() As TimeSpan = { TimeSpan.FromDays(1.5),
TimeSpan.FromHours(1.5),
TimeSpan.FromMinutes(45),
TimeSpan.FromMilliseconds(505),
New TimeSpan(1, 17, 32, 20),
New TimeSpan(-8, 30, 0) }
' Calculate a new time interval by adding each element to the base interval.
For Each interval In intervals
Console.WriteLine("{0,-10:g} {3} {1,15:%d\:hh\:mm\:ss\.ffff} = {2:%d\:hh\:mm\:ss\.ffff}", baseTimeSpan, interval,
baseTimeSpan.Add(interval), If(interval < TimeSpan.Zero, "-", "+"))
Next
End Sub
End Module
' The example displays the following output:
' 1:12:15:16 + 1:12:00:00.0000 = 3:00:15:16.0000
' 1:12:15:16 + 0:01:30:00.0000 = 1:13:45:16.0000
' 1:12:15:16 + 0:00:45:00.0000 = 1:13:00:16.0000
' 1:12:15:16 + 0:00:00:00.5050 = 1:12:15:16.5050
' 1:12:15:16 + 1:17:32:20.0000 = 3:05:47:36.0000
' 1:12:15:16 - 0:07:30:00.0000 = 1:04:45:16.0000
注解
返回值必须介于 和 TimeSpan.MaxValue之间TimeSpan.MinValue;否则会引发异常。
返回值为新的 TimeSpan;原始 TimeSpan 值不会修改。