Provides functionality for measuring and displaying time. The Time type represents an instant in time. Duration represents elapsed time between two instants. Go time durations are typed constants: time.Second, time.Millisecond, etc.
Import: import "time" | Docs: pkg.go.dev/time
| Name | Signature / Value | Description |
|---|---|---|
| time.Now | func Now() Time | Return current local time |
| time.Since | func Since(t Time) Duration | Return elapsed time since t (shorthand for Now().Sub(t)) |
| time.Until | func Until(t Time) Duration | Return duration until t |
| time.Sleep | func Sleep(d Duration) | Pause current goroutine for duration d |
| time.NewTimer | func NewTimer(d Duration) *Timer | Return Timer that fires after duration d |
| time.NewTicker | func NewTicker(d Duration) *Ticker | Return Ticker that fires repeatedly every d |
| time.After | func After(d Duration) <-chan Time | Return channel that receives current time after d |
| time.Parse | func Parse(layout, value string) (Time, error) | Parse time string using reference layout |
| time.ParseDuration | func ParseDuration(s string) (Duration, error) | Parse duration string like '1h30m', '500ms' |
| t.Format | func (t Time) Format(layout string) string | Format time using reference layout |
| t.Add | func (t Time) Add(d Duration) Time | Return time t + d |
| t.Sub | func (t Time) Sub(u Time) Duration | Return duration t-u |
| t.Before | func (t Time) Before(u Time) bool | Report if t is before u |
| t.After | func (t Time) After(u Time) bool | Report if t is after u |
| t.Unix | func (t Time) Unix() int64 | Return Unix time (seconds since Jan 1 1970) |
| t.UnixMilli | func (t Time) UnixMilli() int64 | Return Unix time in milliseconds (Go 1.17+) |
| t.Year/Month/Day | func (t Time) Year() int | Extract calendar components |
| t.Hour/Minute/Second | func (t Time) Hour() int | Extract time-of-day components |
| t.Weekday | func (t Time) Weekday() Weekday | Return day of week (Sunday=0) |
| time.Second | const Duration = 1000000000 | One second as Duration |
| time.Millisecond | const Duration | One millisecond as Duration |
| time.Minute | const Duration | One minute as Duration |
| time.Hour | const Duration | One hour as Duration |
| time.RFC3339 | const layout string | ISO 8601 / RFC 3339 time layout |
| time.RFC822 | const layout string | RFC 822 time layout |
| time.Kitchen | const layout = "3:04PM" | Kitchen clock layout |
Save as main.go inside any directory and run with go run main.go. No extra packages to install — all standard library.
package main
import (
"fmt"
"time"
)
func main() {
// Current time
now := time.Now()
fmt.Println("Now: ", now.Format(time.RFC3339))
fmt.Println("Date: ", now.Format("2006-01-02"))
fmt.Println("Time: ", now.Format("15:04:05"))
fmt.Println("Day: ", now.Weekday())
fmt.Println("Unix: ", now.Unix())
// Duration and sleep
start := time.Now()
time.Sleep(100 * time.Millisecond)
elapsed := time.Since(start)
fmt.Printf("Slept for : %v\n", elapsed)
// Arithmetic
tomorrow := now.Add(24 * time.Hour)
fmt.Println("Tomorrow: ", tomorrow.Format("2006-01-02"))
oneWeekAgo := now.Add(-7 * 24 * time.Hour)
fmt.Println("Week ago: ", oneWeekAgo.Format("2006-01-02"))
// Parse a time string
t, err := time.Parse("2006-01-02", "2000-08-05")
if err == nil {
fmt.Printf("MyWebUniversity founded: %s\n", t.Format("January 2, 2006"))
fmt.Printf("Days since founding: %.0f\n", time.Since(t).Hours()/24)
}
// ParseDuration
d, _ := time.ParseDuration("1h30m45s")
fmt.Printf("Parsed duration: %v\n", d)
// Ticker (fires every 200ms, stop after 3 ticks)
ticker := time.NewTicker(200 * time.Millisecond)
count := 0
for t := range ticker.C {
fmt.Println("Tick at:", t.Format("15:04:05.000"))
count++
if count == 3 {
ticker.Stop()
break
}
}
}
// go run main.go← math | 🏠 Index | net/http →