TimeSpan.Add 메서드
지정된 TimeSpan을 이 인스턴스에 추가합니다.
네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)
구문
‘선언
Public Function Add ( _
ts As TimeSpan _
) As TimeSpan
‘사용 방법
Dim instance As TimeSpan
Dim ts As TimeSpan
Dim returnValue As TimeSpan
returnValue = instance.Add(ts)
public TimeSpan Add (
TimeSpan ts
)
public:
TimeSpan Add (
TimeSpan ts
)
public TimeSpan Add (
TimeSpan ts
)
public function Add (
ts : TimeSpan
) : TimeSpan
매개 변수
- ts
TimeSpan입니다.
반환 값
이 인스턴스의 값과 ts의 값을 더한 값을 나타내는 TimeSpan입니다.
예외
예외 형식 | 조건 |
---|---|
설명
결과 값은 MinValue와 MaxValue 사이의 값이어야 합니다. 그렇지 않으면 예외가 throw됩니다.
반환 값은 새 TimeSpan이며 원래 TimeSpan은 수정되지 않습니다.
예제
다음 코드 예제에서는 몇 개의 TimeSpan 개체 쌍을 만들고 Add 메서드를 사용하여 해당 합을 계산합니다.
' Example of the TimeSpan.Add( ) and TimeSpan.Subtract( ) methods.
Imports System
Imports Microsoft.VisualBasic
Module TimeSpanAddSubDemo
Const dataFmt As String = "{0,-24}{1,24}"
' Pad the end of a TimeSpan string with spaces if it does not
' contain milliseconds.
Function Align( interval As TimeSpan ) As String
Dim intervalStr As String = interval.ToString( )
Dim pointIndex As Integer = intervalStr.IndexOf( ":"c )
pointIndex = intervalStr.IndexOf( "."c, pointIndex )
If pointIndex < 0 Then intervalStr &= " "
Align = intervalStr
End Function
' Display TimeSpan parameters and their sum and difference.
Sub ShowTimeSpanSumDiff( Left as TimeSpan, Right as TimeSpan )
Console.WriteLine( )
Console.WriteLine( dataFmt, "TimeSpan Left", Align( Left ) )
Console.WriteLine( dataFmt, "TimeSpan Right", Align( Right ) )
Console.WriteLine( dataFmt, "Left.Add( Right )", _
Align( Left.Add( Right ) ) )
Console.WriteLine( dataFmt, "Left.Subtract( Right )", _
Align( Left.Subtract( Right ) ) )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the TimeSpan.Add( ) and " & _
"TimeSpan.Subtract( ) " & vbCrLf & "methods " & _
"generates the following output by creating several " & _
vbCrLf & "pairs of TimeSpan objects and calculating " & _
"and displaying " & vbCrLf & "the sum " & _
"and difference of each." )
' Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff( _
new TimeSpan( 1, 20, 0 ), _
new TimeSpan( 0, 45, 10 ) )
ShowTimeSpanSumDiff( _
new TimeSpan( 1, 10, 20, 30, 40 ), _
new TimeSpan( -1, 2, 3, 4, 5 ) )
ShowTimeSpanSumDiff( _
new TimeSpan( 182, 12, 30, 30, 505 ), _
new TimeSpan( 182, 11, 29, 29, 495 ) )
ShowTimeSpanSumDiff( _
new TimeSpan( 888888888888888 ), _
new TimeSpan( 999999999999999 ) )
End Sub
End Module
' This example of the TimeSpan.Add( ) and TimeSpan.Subtract( )
' methods generates the following output by creating several
' pairs of TimeSpan objects and calculating and displaying
' the sum and difference of each.
'
' TimeSpan Left 01:20:00
' TimeSpan Right 00:45:10
' Left.Add( Right ) 02:05:10
' Left.Subtract( Right ) 00:34:50
'
' TimeSpan Left 1.10:20:30.0400000
' TimeSpan Right -21:56:55.9950000
' Left.Add( Right ) 12:23:34.0450000
' Left.Subtract( Right ) 2.08:17:26.0350000
'
' TimeSpan Left 182.12:30:30.5050000
' TimeSpan Right 182.11:29:29.4950000
' Left.Add( Right ) 365.00:00:00
' Left.Subtract( Right ) 01:01:01.0100000
'
' TimeSpan Left 1028.19:21:28.8888888
' TimeSpan Right 1157.09:46:39.9999999
' Left.Add( Right ) 2186.05:08:08.8888887
' Left.Subtract( Right ) -128.14:25:11.1111111
// Example of the TimeSpan.Add( ) and TimeSpan.Subtract( ) methods.
using System;
class TimeSpanAddSubDemo
{
const string dataFmt = "{0,-24}{1,24}" ;
// Pad the end of a TimeSpan string with spaces if it does not
// contain milliseconds.
static string Align( TimeSpan interval )
{
string intervalStr = interval.ToString( );
int pointIndex = intervalStr.IndexOf( ':' );
pointIndex = intervalStr.IndexOf( '.', pointIndex );
if( pointIndex < 0 ) intervalStr += " ";
return intervalStr;
}
// Display TimeSpan parameters and their sum and difference.
static void ShowTimeSpanSumDiff( TimeSpan Left, TimeSpan Right )
{
Console.WriteLine( );
Console.WriteLine( dataFmt, "TimeSpan Left", Align( Left ) );
Console.WriteLine( dataFmt, "TimeSpan Right", Align( Right ) );
Console.WriteLine( dataFmt, "Left.Add( Right )",
Align( Left.Add( Right ) ) );
Console.WriteLine( dataFmt, "Left.Subtract( Right )",
Align( Left.Subtract( Right ) ) );
}
static void Main( )
{
Console.WriteLine(
"This example of the TimeSpan.Add( ) and " +
"TimeSpan.Subtract( ) \nmethods generates the " +
"following output by creating several \npairs of " +
"TimeSpan objects and calculating and displaying \n" +
"the sum and difference of each." );
// Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff(
new TimeSpan( 1, 20, 0 ),
new TimeSpan( 0, 45, 10 ) );
ShowTimeSpanSumDiff(
new TimeSpan( 1, 10, 20, 30, 40 ),
new TimeSpan( -1, 2, 3, 4, 5 ) );
ShowTimeSpanSumDiff(
new TimeSpan( 182, 12, 30, 30, 505 ),
new TimeSpan( 182, 11, 29, 29, 495 ) );
ShowTimeSpanSumDiff(
new TimeSpan( 888888888888888 ),
new TimeSpan( 999999999999999 ) );
}
}
/*
This example of the TimeSpan.Add( ) and TimeSpan.Subtract( )
methods generates the following output by creating several
pairs of TimeSpan objects and calculating and displaying
the sum and difference of each.
TimeSpan Left 01:20:00
TimeSpan Right 00:45:10
Left.Add( Right ) 02:05:10
Left.Subtract( Right ) 00:34:50
TimeSpan Left 1.10:20:30.0400000
TimeSpan Right -21:56:55.9950000
Left.Add( Right ) 12:23:34.0450000
Left.Subtract( Right ) 2.08:17:26.0350000
TimeSpan Left 182.12:30:30.5050000
TimeSpan Right 182.11:29:29.4950000
Left.Add( Right ) 365.00:00:00
Left.Subtract( Right ) 01:01:01.0100000
TimeSpan Left 1028.19:21:28.8888888
TimeSpan Right 1157.09:46:39.9999999
Left.Add( Right ) 2186.05:08:08.8888887
Left.Subtract( Right ) -128.14:25:11.1111111
*/
// Example of the TimeSpan::Add( ) and TimeSpan::Subtract( ) methods.
using namespace System;
// Pad the end of a TimeSpan string with spaces if it does not
// contain milliseconds.
String^ Align( TimeSpan interval )
{
String^ intervalStr = interval.ToString();
int pointIndex = intervalStr->IndexOf( ':' );
pointIndex = intervalStr->IndexOf( '.', pointIndex );
if ( pointIndex < 0 )
intervalStr = String::Concat( intervalStr, " " );
return intervalStr;
}
// Display TimeSpan parameters and their sum and difference.
static void ShowTimeSpanSumDiff( TimeSpan Left, TimeSpan Right )
{
String^ dataFmt = "{0,-24}{1,24}";
Console::WriteLine();
Console::WriteLine( dataFmt, "TimeSpan Left", Align( Left ) );
Console::WriteLine( dataFmt, "TimeSpan Right", Align( Right ) );
Console::WriteLine( dataFmt, "Left.Add( Right )", Align( Left.Add( Right ) ) );
Console::WriteLine( dataFmt, "Left.Subtract( Right )", Align( Left.Subtract( Right ) ) );
}
int main()
{
Console::WriteLine( "This example of the TimeSpan::Add( ) and "
"TimeSpan::Subtract( ) \nmethods generates the "
"following output by creating several \npairs of "
"TimeSpan objects and calculating and displaying \n"
"the sum and difference of each." );
// Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff( TimeSpan(1,20,0), TimeSpan(0,45,10) );
ShowTimeSpanSumDiff( TimeSpan(1,10,20,30,40), TimeSpan( -1,2,3,4,5) );
ShowTimeSpanSumDiff( TimeSpan(182,12,30,30,505), TimeSpan(182,11,29,29,495) );
ShowTimeSpanSumDiff( TimeSpan(888888888888888), TimeSpan(999999999999999) );
}
/*
This example of the TimeSpan::Add( ) and TimeSpan::Subtract( )
methods generates the following output by creating several
pairs of TimeSpan objects and calculating and displaying
the sum and difference of each.
TimeSpan Left 01:20:00
TimeSpan Right 00:45:10
Left.Add( Right ) 02:05:10
Left.Subtract( Right ) 00:34:50
TimeSpan Left 1.10:20:30.0400000
TimeSpan Right -21:56:55.9950000
Left.Add( Right ) 12:23:34.0450000
Left.Subtract( Right ) 2.08:17:26.0350000
TimeSpan Left 182.12:30:30.5050000
TimeSpan Right 182.11:29:29.4950000
Left.Add( Right ) 365.00:00:00
Left.Subtract( Right ) 01:01:01.0100000
TimeSpan Left 1028.19:21:28.8888888
TimeSpan Right 1157.09:46:39.9999999
Left.Add( Right ) 2186.05:08:08.8888887
Left.Subtract( Right ) -128.14:25:11.1111111
*/
// Example of the TimeSpan.Add( ) and TimeSpan.Subtract( ) methods.
import System.*;
class TimeSpanAddSubDemo
{
private static String dataFmt = "{0,-24}{1,24}";
// Pad the end of a TimeSpan string with spaces if it does not
// contain milliseconds.
static String Align(TimeSpan interval)
{
String intervalStr = interval.ToString();
int pointIndex = intervalStr.IndexOf(':');
pointIndex = intervalStr.IndexOf('.', pointIndex);
if (pointIndex < 0) {
intervalStr += " ";
}
return intervalStr;
} //Align
// Display TimeSpan parameters and their sum and difference.
static void ShowTimeSpanSumDiff(TimeSpan Left, TimeSpan Right)
{
Console.WriteLine();
Console.WriteLine(dataFmt, "TimeSpan Left", Align(Left));
Console.WriteLine(dataFmt, "TimeSpan Right", Align(Right));
Console.WriteLine(dataFmt, "Left.Add( Right )",
Align(Left.Add(Right)));
Console.WriteLine(dataFmt, "Left.Subtract( Right )",
Align(Left.Subtract(Right)));
} //ShowTimeSpanSumDiff
public static void main(String[] args)
{
Console.WriteLine(("This example of the TimeSpan.Add( ) and "
+ "TimeSpan.Subtract( ) \nmethods generates the "
+ "following output by creating several \npairs of "
+ "TimeSpan objects and calculating and displaying \n"
+ "the sum and difference of each."));
// Create pairs of TimeSpan objects.
ShowTimeSpanSumDiff(new TimeSpan(1, 20, 0), new TimeSpan(0, 45, 10));
ShowTimeSpanSumDiff(new TimeSpan(1, 10, 20, 30, 40),
new TimeSpan(-1, 2, 3, 4, 5));
ShowTimeSpanSumDiff(new TimeSpan(182, 12, 30, 30, 505),
new TimeSpan(182, 11, 29, 29, 495));
ShowTimeSpanSumDiff(new TimeSpan(888888888888888L),
new TimeSpan(999999999999999L));
} //main
} //TimeSpanAddSubDemo
/*
This example of the TimeSpan.Add( ) and TimeSpan.Subtract( )
methods generates the following output by creating several
pairs of TimeSpan objects and calculating and displaying
the sum and difference of each.
TimeSpan Left 01:20:00
TimeSpan Right 00:45:10
Left.Add( Right ) 02:05:10
Left.Subtract( Right ) 00:34:50
TimeSpan Left 1.10:20:30.0400000
TimeSpan Right -21:56:55.9950000
Left.Add( Right ) 12:23:34.0450000
Left.Subtract( Right ) 2.08:17:26.0350000
TimeSpan Left 182.12:30:30.5050000
TimeSpan Right 182.11:29:29.4950000
Left.Add( Right ) 365.00:00:00
Left.Subtract( Right ) 01:01:01.0100000
TimeSpan Left 1028.19:21:28.8888888
TimeSpan Right 1157.09:46:39.9999999
Left.Add( Right ) 2186.05:08:08.8888887
Left.Subtract( Right ) -128.14:25:11.1111111
*/
플랫폼
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.
버전 정보
.NET Framework
2.0, 1.1, 1.0에서 지원
.NET Compact Framework
2.0, 1.0에서 지원