English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutoriales Básicos de Golang

Sentencias de Control de Golang

Funciones & Métodos de Golang

Estructuras de Golang

Cortes & Arreglos de Golang

Cadenas (String) de Golang

Punteros de Golang

Interfaces de Golang

Concurrencia de Golang

Excepciones (Error) de Golang

Misceláneos de Golang

Fecha y hora (Time) del lenguaje Go

Go tiene un excelente soporte para operaciones de tiempo. El tiempo de la era Unix se utiliza como referencia para las operaciones de tiempo.

podemos usar el método Date proporcionado por el paquete time para construir objetos de tiempo. Este paquete contiene métodos como year(), month(), day(), location() y otros.

usamos objetos de tiempo para llamar a estos métodos.

Ejemplo de tiempo en Go

package main
import "fmt"
import "time"
func main() {
	p := fmt.Println
	present := time.Now()//tiempo actual
	p(present)
	DOB := time.Date(1993, 02, 28, 9,04,39,213 ,time.Local)
	fmt.Println(DOB)
	fmt.Println(DOB.Year())
	fmt.Println(DOB.Month())
	fmt.Println(DOB.Day())
	fmt.Println(DOB.Hour())
	fmt.Println(DOB.Minute())
	fmt.Println(DOB.Second())
	fmt.Println(DOB.Nanosecond())
	fmt.Println(DOB.Location())
	fmt.Println(DOB.Weekday())
	fmt.Println(DOB.Before(present))
	
	fmt.Println(DOB.After(present))
	fmt.Println(DOB.Equal(present))
	diff := present.Sub(DOB)
	fmt.Println(diff)
	fmt.Println(diff.Hours())
	fmt.Println(diff.Minutes())
	fmt.Println(diff.Seconds())
	fmt.Println(diff.Nanoseconds())
	fmt.Println(DOB.Add(diff))-fmt.Println(DOB.Add(
}

Salida:

2017-10-04 17:10:13.474931994 +053diff))+0 IST m=334969
1993-02-28 09:04:390.000213 +0530 IST
1993
.000000
28
9
4
39
213
Local
domingo
verdadero
falso
falso
215624h5m34.474931781s
215624.09290970326
1.2937445574582197e+07
7.762467344749318e+08
776246734474931781
2017-10-04 17:10:13.474931994 +0530 IST
1968-07-25 00:59:04.525068432 +0530 IST
Proceso finalizado con código de salida 0

Ejemplo de tiempo en Go2

package main
import (
	"fmt"
	"time"
)
func main() {
	present := time.Now()
	fmt.Println("Hoy : ", present.Format("Lun, Ene 2, 2006 en 3:04p.m."))
	someTime := time.Date(2017, time.March 30 11, 30 55, 123456, time.Local)
	// Usar time.Equal() para comparar fechas
	sameTime := someTime.Equal(present)
	fmt.Println("someTime equals to now ? : ", sameTime)
	//Calcular la diferencia de tiempo entre hoy y antes
	diff := present.Sub(someTime)
	//Convertir la diferencia en días
	days := int(diff.Hours()) / 24)
	fmt.Printf("30 de marzo 2017 era hace %d días %s

}

Salida:

Hoy : Mié, Oct 4, 2017 en 5:15p.m.
someTime equals to now ? : false
30 de marzo 2017 era 188 hace días