Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

TimeSpan.Negate Method

Returns a TimeSpan whose value is the negated value of this instance.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public TimeSpan Negate ()
public TimeSpan Negate ()
public function Negate () : TimeSpan

Return Value

The same numeric value as this instance, but with the opposite sign.
Exception typeCondition

OverflowException

The negated value of this instance cannot be represented by a TimeSpan; that is, the value of this instance is MinValue.

The following code example applies the Negate method to several TimeSpan objects.

// Example of the TimeSpan.Duration( ) and TimeSpan.Negate( ) methods,
// and the TimeSpan Unary Negation and Unary Plus operators.
using System;

class DuraNegaUnaryDemo
{
    const string dataFmt = "{0,22}{1,22}{2,22}" ;

    static void ShowDurationNegate( TimeSpan interval )
    {
        // Display the TimeSpan value and the results of the 
        // Duration and Negate methods.
        Console.WriteLine( dataFmt, 
            interval, interval.Duration( ), interval.Negate( ) );
    }

    static void Main()
    {
        Console.WriteLine(
            "This example of TimeSpan.Duration( ), " +
            "TimeSpan.Negate( ), \nand the TimeSpan Unary " +
            "Negation and Unary Plus operators \n" +
            "generates the following output.\n" );
        Console.WriteLine( dataFmt, 
            "TimeSpan", "Duration( )", "Negate( )" );
        Console.WriteLine( dataFmt, 
            "--------", "-----------", "---------" );

        // Create TimeSpan objects and apply the Unary Negation
        // and Unary Plus operators to them.
        ShowDurationNegate( new TimeSpan( 1 ) );
        ShowDurationNegate( new TimeSpan( -1234567 ) );
        ShowDurationNegate( 
            + new TimeSpan( 0, 0, 10, -20, -30 ) );
        ShowDurationNegate( 
            + new TimeSpan( 0, -10, 20, -30, 40 ) );
        ShowDurationNegate( 
            - new TimeSpan( 1, 10, 20, 40, 160 ) );
        ShowDurationNegate( 
            - new TimeSpan( -10, -20, -30, -40, -50 ) );
    } 
} 

/*
This example of TimeSpan.Duration( ), TimeSpan.Negate( ),
and the TimeSpan Unary Negation and Unary Plus operators
generates the following output.

              TimeSpan           Duration( )             Negate( )
              --------           -----------             ---------
      00:00:00.0000001      00:00:00.0000001     -00:00:00.0000001
     -00:00:00.1234567      00:00:00.1234567      00:00:00.1234567
      00:09:39.9700000      00:09:39.9700000     -00:09:39.9700000
     -09:40:29.9600000      09:40:29.9600000      09:40:29.9600000
   -1.10:20:40.1600000    1.10:20:40.1600000    1.10:20:40.1600000
   10.20:30:40.0500000   10.20:30:40.0500000  -10.20:30:40.0500000
*/ 

// Example of the TimeSpan.Duration( ) and TimeSpan.Negate( ) methods,
// and the TimeSpan Unary Negation and Unary Plus operators.
import System.*;

class DuraNegaUnaryDemo
{
    private static String dataFmt = "{0,22}{1,22}{2,22}";

    static void ShowDurationNegate(TimeSpan interval)
    {
        // Display the TimeSpan value and the results of the 
        // Duration and Negate methods.
        Console.WriteLine(dataFmt, interval, interval.Duration(),
            interval.Negate());
    } //ShowDurationNegate

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of TimeSpan.Duration( ), " 
            + "TimeSpan.Negate( ), \nand the TimeSpan Unary " 
            + "Negation and Unary Plus operators \n" 
            + "generates the following output.\n"));
        Console.WriteLine(dataFmt, "TimeSpan", "Duration( )", "Negate( )");
        Console.WriteLine(dataFmt, "--------", "-----------", "---------");

        // Create TimeSpan objects and apply the Unary Negation
        // and Unary Plus operators to them.
        ShowDurationNegate(new TimeSpan(1));
        ShowDurationNegate(new TimeSpan(-1234567));
        ShowDurationNegate(new TimeSpan(0, 0, 10, -20, -30));
        ShowDurationNegate(new TimeSpan(0, -10, 20, -30, 40));
        ShowDurationNegate(new TimeSpan(1, 10, 20, 40, 160).Negate());
        ShowDurationNegate(new TimeSpan(-10, -20, -30, -40, -50).Negate());
    } //main
} //DuraNegaUnaryDemo

/*
This example of TimeSpan.Duration( ), TimeSpan.Negate( ),
and the TimeSpan Unary Negation and Unary Plus operators
generates the following output.

              TimeSpan           Duration( )             Negate( )
              --------           -----------             ---------
      00:00:00.0000001      00:00:00.0000001     -00:00:00.0000001
     -00:00:00.1234567      00:00:00.1234567      00:00:00.1234567
      00:09:39.9700000      00:09:39.9700000     -00:09:39.9700000
     -09:40:29.9600000      09:40:29.9600000      09:40:29.9600000
   -1.10:20:40.1600000    1.10:20:40.1600000    1.10:20:40.1600000
   10.20:30:40.0500000   10.20:30:40.0500000  -10.20:30:40.0500000
*/

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.