DateTimeOffset.AddYears(Int32) Método
Definição
Retorna um novo objeto DateTimeOffset que adiciona um número especificado de anos ao valor dessa instância.Returns a new DateTimeOffset object that adds a specified number of years to the value of this instance.
public:
DateTimeOffset AddYears(int years);
public DateTimeOffset AddYears (int years);
member this.AddYears : int -> DateTimeOffset
Public Function AddYears (years As Integer) As DateTimeOffset
Parâmetros
- years
- Int32
Um número de anos.A number of years. O número pode ser positivo ou negativo.The number can be negative or positive.
Retornos
Um objeto cujo valor é a soma da data e hora representada pelo objeto DateTimeOffset atual e o número de anos representado por years.An object whose value is the sum of the date and time represented by the current DateTimeOffset object and the number of years represented by years.
Exceções
O valor DateTimeOffset resultante é menor que MinValue.The resulting DateTimeOffset value is less than MinValue.
- ou --or- O valor DateTimeOffset resultante é maior que MaxValue.The resulting DateTimeOffset value is greater than MaxValue.
Exemplos
No Estados Unidos, as licenças do driver não podem ser emitidas para pessoas menos de 16 anos de idade.In the United States, driver's licenses cannot be issued to persons under 16 years of age. O exemplo a seguir exibe a data mais recente possível na qual uma pessoa deve ser nasceu para que seja emitida legalmente a licença de um driver.The following example displays the latest possible date on which a person must be born in order to legally be issued a driver's license.
const int minimumAge = 16;
DateTimeOffset dateToday = DateTimeOffset.Now;
DateTimeOffset latestBirthday = dateToday.AddYears(-1 * minimumAge);
Console.WriteLine("To possess a driver's license, you must have been born on or before {0:d}.",
latestBirthday);
Const minimumAge As Integer = 16
Dim dateToday As DateTimeOffset = DateTimeOffset.Now
Dim latestBirthday As DateTimeOffset = dateToday.AddYears(-1 * minimumAge)
Console.WriteLine("To possess a driver's license, you must have been born on or before {0:d}.", _
latestBirthday)
Comentários
Ao contrário da maioria dos outros métodos que adicionam uma única unidade de intervalo de tempo (como minutos ou dias) a um valor de data e hora, AddYears o não permite que você adicione partes fracionárias de um ano.Unlike most of the other methods that add a single time interval unit (such as minutes or days) to a date and time value, AddYears does not enable you to add fractional parts of a year. Para adicionar uma hora que consiste em outras unidades de tempo, além de anos em um DateTimeOffset objeto, use o Add método.To add a time that consists of other time units in addition to years to a DateTimeOffset object, use the Add method.
Observação
Este método retorna um novo objeto DateTimeOffset.This method returns a new DateTimeOffset object. Ele não modifica o valor do objeto atual adicionando months à sua data e hora.It does not modify the value of the current object by adding months to its date and time.