En el lenguaje Go, los paquetes de tiempo brindan funcionalidad para determinar y ver el tiempo. La función Time.AddDate() en el lenguaje Go se utiliza para comprobar la hora, lo que equivale a sumar el número indicado de años, meses y días a la «t» dada.
Por ejemplo, si los parámetros del método son como (-2, 4, 5) y la t indicada es el 3 de febrero de 2018, la salida será el 8 de junio de 2016. Me gusta el método Fecha aquí también si el rango de meses, días o años están fuera del rango normal, entonces se convierte automáticamente al rango normalizado. Además, esta función se define en el paquete de tiempo. Aquí, debe importar el paquete «tiempo» para poder usar estas funciones.
Sintaxis:
func (t Time) AddDate(years int, months int, days int) Time
Aquí, “t” es el tiempo establecido.
Valor devuelto: Devuelve el resultado de sumar el t indicado al número indicado de años, meses y días.
Ejemplo 1:
// Golang program to illustrate the usage of // Time.AddDate() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Declaring Time in UTC Time := time.Date(2018, 6, 4, 0, 0, 0, 0, time.UTC) // Calling AddDate method with all // its parameters t1 := Time.AddDate(1, 2, 5) t2 := Time.AddDate(5, -2, 9) t3 := Time.AddDate(0, 3, -3) t4 := Time.AddDate(1, 0, 0) // Prints output fmt.Printf("%v\n", Time) fmt.Printf("%v\n", t1) fmt.Printf("%v\n", t2) fmt.Printf("%v\n", t3) fmt.Printf("%v\n", t4) }
Producción:
2018-06-04 00:00:00 +0000 UTC 2019-08-09 00:00:00 +0000 UTC 2023-04-13 00:00:00 +0000 UTC 2018-09-01 00:00:00 +0000 UTC 2019-06-04 00:00:00 +0000 UTC
Aquí, la salida devuelta está en UTC como se definió anteriormente.
Ejemplo 2:
// Golang program to illustrate the usage of // Time.AddDate() function // Including main package package main // Importing fmt and time import "fmt" import "time" // Calling main func main() { // Declaring Time in UTC Time := time.Date(2020, 15, 34, 0, 0, 0, 0, time.UTC) // Calling AddDate method with all // its parameters t1 := Time.AddDate(3, 13, 35) t2 := Time.AddDate(2, -24, 29) t3 := Time.AddDate(4, 32, -31) t4 := Time.AddDate(5, 10, -11) // Prints output fmt.Printf("%v\n", Time) fmt.Printf("%v\n", t1) fmt.Printf("%v\n", t2) fmt.Printf("%v\n", t3) fmt.Printf("%v\n", t4) }
Producción:
2021-04-03 00:00:00 +0000 UTC 2025-06-07 00:00:00 +0000 UTC 2021-05-02 00:00:00 +0000 UTC 2027-11-02 00:00:00 +0000 UTC 2027-01-23 00:00:00 +0000 UTC
Aquí, el rango utilizado está fuera del rango habitual, pero se normalizan durante la conversión.
Publicación traducida automáticamente
Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA