DateTime.Subtract Método

Definição

Retorna o valor resultante da subtração do tempo especificado ou da duração do valor dessa instância.Returns the value that results from subtracting the specified time or duration from the value of this instance.

Sobrecargas

Subtract(DateTime)

Retorna um novo TimeSpan que subtrai a data e hora especificados do valor dessa instância.Returns a new TimeSpan that subtracts the specified date and time from the value of this instance.

Subtract(TimeSpan)

Retorna um novo DateTime que subtrai a duração especificada do valor dessa instância.Returns a new DateTime that subtracts the specified duration from the value of this instance.

Subtract(DateTime)

Retorna um novo TimeSpan que subtrai a data e hora especificados do valor dessa instância.Returns a new TimeSpan that subtracts the specified date and time from the value of this instance.

public:
 TimeSpan Subtract(DateTime value);
public TimeSpan Subtract (DateTime value);
member this.Subtract : DateTime -> TimeSpan
Public Function Subtract (value As DateTime) As TimeSpan

Parâmetros

value
DateTime

O valor de data e hora a ser subtraído.The date and time value to subtract.

Retornos

TimeSpan

Um intervalo de tempo igual à data e hora representada por essa instância menos a data e a hora representada por value.A time interval that is equal to the date and time represented by this instance minus the date and time represented by value.

Exceções

O resultado é menor que MinValue ou maior que MaxValue.The result is less than MinValue or greater than MaxValue.

Exemplos

O exemplo a seguir demonstra o método Subtract e o operador de subtração.The following example demonstrates the Subtract method and the subtraction operator.

System::DateTime date1 = System::DateTime( 1996, 6, 3, 22, 15, 0 );
System::DateTime date2 = System::DateTime( 1996, 12, 6, 13, 2, 0 );
System::DateTime date3 = System::DateTime( 1996, 10, 12, 8, 42, 0 );

// diff1 gets 185 days, 14 hours, and 47 minutes.
System::TimeSpan diff1 = date2.Subtract( date1 );

// date4 gets 4/9/1996 5:55:00 PM.
System::DateTime date4 = date3.Subtract( diff1 );

// diff2 gets 55 days 4 hours and 20 minutes.
System::TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System::DateTime date5 = date1 - diff2;
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);

// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);

// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0)

Dim diff1 As System.TimeSpan
' diff1 gets 185 days, 14 hours, and 47 minutes.
diff1 = date2.Subtract(date1)

Dim date4 As System.DateTime
' date4 gets 4/9/1996 5:55:00 PM.
date4 = date3.Subtract(diff1)

Dim diff2 As System.TimeSpan
' diff2 gets 55 days 4 hours and 20 minutes.
diff2 = System.DateTime.op_Subtraction(date2, date3)

Dim date5 As System.DateTime
' date5 gets 4/9/1996 5:55:00 PM.
date5 = System.DateTime.op_Subtraction(date1, diff2)

Comentários

O Subtract(DateTime) método determina a diferença entre duas datas.The Subtract(DateTime) method determines the difference between two dates. Para subtrair um intervalo de tempo da instância atual, chame o Subtract(TimeSpan) método.To subtract a time interval from the current instance, call the Subtract(TimeSpan) method. Para subtrair um intervalo de tempo específico da instância atual, chame o método que adiciona esse intervalo de tempo à data atual e forneça um valor negativo como o argumento do método.To subtract a particular time interval from the current instance, call the method that adds that time interval to the current date, and supply a negative value as the method argument. Por exemplo, para subtrair dois meses da data atual, chame o AddMonths(Int32) método com um valor de-2.For example, to subtract two months from the current date, call the AddMonths(Int32) method with a value of -2.

Se a data e hora da instância atual for anterior a value , o método retornará um TimeSpan objeto que representa um intervalo de tempo negativo.If the date and time of the current instance is earlier than value, the method returns a TimeSpan object that represents a negative time span. Ou seja, o valor de todas as suas propriedades diferentes de zero (como Days ou Ticks ) é negativo.That is, the value of all of its non-zero properties (such as Days or Ticks) is negative.

O Subtract(DateTime) método não considera o valor da Kind propriedade dos dois DateTime valores ao executar a subtração.The Subtract(DateTime) method does not consider the value of the Kind property of the two DateTime values when performing the subtraction. Antes de subtrair DateTime objetos, verifique se os objetos representam horas no mesmo fuso horário.Before subtracting DateTime objects, ensure that the objects represent times in the same time zone. Caso contrário, o resultado incluirá a diferença entre os fusos horários.Otherwise, the result will include the difference between time zones.

Observação

O DateTimeOffset.Subtract(DateTimeOffset) método considera a diferença entre os fusos horários durante a execução da subtração.The DateTimeOffset.Subtract(DateTimeOffset) method does consider the difference between time zones when performing the subtraction.

Aplica-se a

Subtract(TimeSpan)

Retorna um novo DateTime que subtrai a duração especificada do valor dessa instância.Returns a new DateTime that subtracts the specified duration from the value of this instance.

public:
 DateTime Subtract(TimeSpan value);
public DateTime Subtract (TimeSpan value);
member this.Subtract : TimeSpan -> DateTime
Public Function Subtract (value As TimeSpan) As DateTime

Parâmetros

value
TimeSpan

O intervalo de tempo a ser subtraído.The time interval to subtract.

Retornos

DateTime

Um objeto igual à data e hora representadas por essa instância menos o intervalo de tempo representado por value.An object that is equal to the date and time represented by this instance minus the time interval represented by value.

Exceções

O resultado é menor que MinValue ou maior que MaxValue.The result is less than MinValue or greater than MaxValue.

Exemplos

O exemplo a seguir demonstra o método Subtract e o operador de subtração.The following example demonstrates the Subtract method and the subtraction operator.

System::DateTime date1 = System::DateTime( 1996, 6, 3, 22, 15, 0 );
System::DateTime date2 = System::DateTime( 1996, 12, 6, 13, 2, 0 );
System::DateTime date3 = System::DateTime( 1996, 10, 12, 8, 42, 0 );

// diff1 gets 185 days, 14 hours, and 47 minutes.
System::TimeSpan diff1 = date2.Subtract( date1 );

// date4 gets 4/9/1996 5:55:00 PM.
System::DateTime date4 = date3.Subtract( diff1 );

// diff2 gets 55 days 4 hours and 20 minutes.
System::TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System::DateTime date5 = date1 - diff2;
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);

// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);

// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;

// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim date3 As New System.DateTime(1996, 10, 12, 8, 42, 0)

Dim diff1 As System.TimeSpan
' diff1 gets 185 days, 14 hours, and 47 minutes.
diff1 = date2.Subtract(date1)

Dim date4 As System.DateTime
' date4 gets 4/9/1996 5:55:00 PM.
date4 = date3.Subtract(diff1)

Dim diff2 As System.TimeSpan
' diff2 gets 55 days 4 hours and 20 minutes.
diff2 = System.DateTime.op_Subtraction(date2, date3)

Dim date5 As System.DateTime
' date5 gets 4/9/1996 5:55:00 PM.
date5 = System.DateTime.op_Subtraction(date1, diff2)

Comentários

O Subtract(TimeSpan) método retorna a data que é uma diferença de intervalo de tempo especificada da instância atual.The Subtract(TimeSpan) method returns the date that is a specified time interval difference from the current instance. Para determinar o intervalo de tempo entre duas datas, chame o Subtract(DateTime) método.To determine the time interval between two dates, call the Subtract(DateTime) method. Para subtrair um intervalo de tempo específico da instância atual, chame o método que adiciona esse intervalo de tempo à data atual e forneça um valor negativo como o argumento do método.To subtract a particular time interval from the current instance, call the method that adds that time interval to the current date, and supply a negative value as the method argument. Por exemplo, para subtrair dois meses da data atual, chame o AddMonths(Int32) método com um valor de-2.For example, to subtract two months from the current date, call the AddMonths(Int32) method with a value of -2.

Este método não altera o valor desse DateTime.This method does not change the value of this DateTime. Em vez disso, ele retorna um novo DateTime cujo valor é o resultado dessa operação.Instead, it returns a new DateTime whose value is the result of this operation.

Normalmente, o DateTime.Subtract(TimeSpan) método subtrai um TimeSpan objeto que representa um intervalo de tempo positivo e retorna um DateTime valor que é anterior à data e hora da instância atual.Ordinarily, the DateTime.Subtract(TimeSpan) method subtracts a TimeSpan object that represents a positive time span and returns a DateTime value that is earlier than the date and time of the current instance. No entanto, se o TimeSpan objeto representar um intervalo de tempo negativo, o DateTime.Subtract(TimeSpan) método retornará um DateTime valor posterior à data e hora da instância atual.However, if the TimeSpan object represents a negative time span, the DateTime.Subtract(TimeSpan) method returns a DateTime value that is later than the date and time of the current instance.

O DateTime.Subtract(TimeSpan) método permite subtrair um intervalo de tempo que consiste em mais de uma unidade de tempo (como um determinado número de horas e um determinado número de minutos).The DateTime.Subtract(TimeSpan) method allows you to subtract a time interval that consists of more than one unit of time (such as a given number of hours and a given number of minutes). Para subtrair uma única unidade de tempo (como anos, meses ou dias) da DateTime instância, você pode passar um valor numérico negativo como um parâmetro para qualquer um dos seguintes métodos:To subtract a single unit of time (such as years, months, or days) from the DateTime instance, you can pass a negative numeric value as a parameter to any of the following methods:

  • AddYears, para subtrair um número específico de anos da instância atual de data e hora.AddYears, to subtract a specific number of years from the current date and time instance.

  • AddMonths, para subtrair um número específico de meses da instância atual de data e hora.AddMonths, to subtract a specific number of months from the current date and time instance.

  • AddDays, para subtrair um número específico de dias da instância atual de data e hora.AddDays, to subtract a specific number of days from the current date and time instance.

  • AddHours, para subtrair um número específico de horas da instância atual de data e hora.AddHours, to subtract a specific number of hours from the current date and time instance.

  • AddMinutes, para subtrair um número específico de minutos da instância atual de data e hora.AddMinutes, to subtract a specific number of minutes from the current date and time instance.

  • AddSeconds, para subtrair um número específico de segundos da instância atual de data e hora.AddSeconds, to subtract a specific number of seconds from the current date and time instance.

  • AddMilliseconds, para subtrair um número específico de milissegundos da instância atual de data e hora.AddMilliseconds, to subtract a specific number of milliseconds from the current date and time instance.

  • AddTicks, para subtrair um número específico de tiques da instância atual de data e hora.AddTicks, to subtract a specific number of ticks from the current date and time instance.

Confira também

Aplica-se a