función time.Time.Before() en Golang con ejemplos

En el lenguaje Go, los paquetes de tiempo brindan funcionalidad para determinar y ver el tiempo. La función Time.Before() en el lenguaje Go se usa para comprobar si el instante de tiempo indicado t es anterior a u indicado o no. Además, esta función se define en el paquete de tiempo. Aquí, debe importar el paquete «tiempo» para usar estas funciones.

Sintaxis:

func (t Time) Before(u Time) bool

Aquí, “t” es el tiempo indicado y “u” es el tiempo que está presente como argumento en el método Before().

Valor devuelto: Devuelve verdadero si «t» está presente antes de «u», de lo contrario, devuelve falso.

Ejemplo 1:

// Golang program to illustrate the usage of
// Time.Before() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t and u in UTC
    t := time.Date(2019, 0, 0, 0, 0, 0, 0, time.UTC)
    u := time.Date(2020, 0, 0, 0, 0, 0, 0, time.UTC)
  
    // Calling Before method
    res := t.Before(u)
  
    // Prints output
    fmt.Printf("%v", res)
}

Producción:

true

Ejemplo 2:

// Golang program to illustrate the usage of
// Time.Before() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Declaring t and u in UTC
    t := time.Date(2030, 0, 0, 0, 0, 0, 0, time.UTC)
    u := time.Date(2025, 0, 0, 0, 0, 0, 0, time.UTC)
  
    // Calling Before method
    res := t.Before(u)
  
    // Prints output
    fmt.Printf("%v", res)
}

Producción:

false

Publicación traducida automáticamente

Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *