Go
Time Handling
Common operations in the time package.
By EZ4Code Team
timetime
Code
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
fmt.Println(now)
// Format (must use 2006-01-02 15:04:05)
fmt.Println(now.Format("2006-01-02 15:04:05"))
// Parse
t, _ := time.Parse("2006-01-02", "2024-01-15")
fmt.Println(t)
// Time difference
diff := now.Sub(t)
fmt.Println(diff.Hours())
// Timer
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for i := 0; i < 3; i++ {
<-ticker.C
fmt.Println("tick")
}
}Explanation
Go time formatting uses the specific reference time 2006-01-02 15:04:05.