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

Golang 基础教程

Golang 控制语句

Golang 函数 & 方法

Golang 结构体

Golang 切片 & 数组

Strings (String) in Golang

Pointers in Golang

Interfaces in Golang

Concurrency in Golang

Golang exceptions (Error)

Miscellaneous items in Golang

Data e ora del linguaggio Go (Time)

Go has good support for time operations. Unix epoch time is used as the reference for time operations.

We can use the Date method provided by the time package to build time objects. The package includes methods such as year(), month(), day(), location(), and so on.

We can call these methods by using time objects.

Go time example

package main
import "fmt"
import "time"
func main() {
	p := fmt.Println
	present := time.Now()//current time
	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(-diff))
}

Output:

04-10-2017 17:10:13.474931994 +0530 IST m=+0.000334969
1993-02-28 09:04:39.000000213 +0530 IST
1993
Febbraio
28
9
4
39
213
Locale
Domenica
true
false
false
215624h5m34.474931781s
215624.09290970326
1.2937445574582197e+07
7.762467344749318e+08
776246734474931781
4 ottobre 2017 17:10:13.474931994 +0530 IST
25 luglio 1968 00:59:04.525068432 +0530 IST
Processo completato con codice di uscita 0

Esempio di tempo Go 2

package main
import (
	"fmt"
	"time"
)
func main() {
	present := time.Now()
	fmt.Println("Oggi : ", present.Format("Mon, Jan 2, 2006 alle 15:04"))
	someTime := time.Date(2017, time.March, 30, 11, 30, 55, 123456, time.Local)
	//Usare time.Equal() per confrontare i tempi
	sameTime := someTime.Equal(present)
	fmt.Println("someTime uguale a now ? : ", sameTime)
	//Calcolare la differenza tra oggi e in precedenza
	diff := present.Sub(someTime)
	//Convertire la differenza in giorni
	days := int(diff.Hours() / 24)
	fmt.Printf("30 marzo 2017 era %d giorni fa \n", days)
}

Output:

Oggi : Mer, 4 ott 2017 alle 17:15
someTime uguale a now ? : false
30 marzo 2017 era 188 giorni fa