Exportar (0) Imprimir
Expandir todo

TimeSpan.Add (Método)

Actualización: noviembre 2007

Agrega el TimeSpan especificado a esta instancia.

Espacio de nombres:  System
Ensamblado:  mscorlib (en mscorlib.dll)

public TimeSpan Add(
	TimeSpan ts
)
public TimeSpan Add(
	TimeSpan ts
)
public function Add(
	ts : TimeSpan
) : TimeSpan

Parámetros

ts
Tipo: System.TimeSpan
TimeSpan.

Valor devuelto

Tipo: System.TimeSpan
TimeSpan que representa el valor de esta instancia más el valor de ts.

ExcepciónCondición
OverflowException

El objeto TimeSpan resultante es menor que MinValue o mayor que MaxValue.

El valor devuelto debe estar comprendido entre MinValue y MaxValue; en caso contrario, se produce una excepción.

El valor devuelto es un objeto TimeSpan nuevo; el objeto TimeSpan original no se modifica.

En el siguiente ejemplo de código se crean varios pares de objetos TimeSpan y se calcula su suma mediante el método Add.

// 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.
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 Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile para Smartphone, Windows Mobile para Pocket PC, Xbox 360

.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.

.NET Framework

Compatible con: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 3.5, 2.0, 1.0

XNA Framework

Compatible con: 2.0, 1.0

Adiciones de comunidad

AGREGAR
Mostrar:
© 2014 Microsoft