Languages: Python C C++ Go Java Ruby Rust Perl R ← Full TOC

← Go Index

🕐 time — Time, duration, timers and sleep

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

📋 Functions, Types & Constants

NameSignature / ValueDescription
time.Nowfunc Now() TimeReturn current local time
time.Sincefunc Since(t Time) DurationReturn elapsed time since t (shorthand for Now().Sub(t))
time.Untilfunc Until(t Time) DurationReturn duration until t
time.Sleepfunc Sleep(d Duration)Pause current goroutine for duration d
time.NewTimerfunc NewTimer(d Duration) *TimerReturn Timer that fires after duration d
time.NewTickerfunc NewTicker(d Duration) *TickerReturn Ticker that fires repeatedly every d
time.Afterfunc After(d Duration) <-chan TimeReturn channel that receives current time after d
time.Parsefunc Parse(layout, value string) (Time, error)Parse time string using reference layout
time.ParseDurationfunc ParseDuration(s string) (Duration, error)Parse duration string like '1h30m', '500ms'
t.Formatfunc (t Time) Format(layout string) stringFormat time using reference layout
t.Addfunc (t Time) Add(d Duration) TimeReturn time t + d
t.Subfunc (t Time) Sub(u Time) DurationReturn duration t-u
t.Beforefunc (t Time) Before(u Time) boolReport if t is before u
t.Afterfunc (t Time) After(u Time) boolReport if t is after u
t.Unixfunc (t Time) Unix() int64Return Unix time (seconds since Jan 1 1970)
t.UnixMillifunc (t Time) UnixMilli() int64Return Unix time in milliseconds (Go 1.17+)
t.Year/Month/Dayfunc (t Time) Year() intExtract calendar components
t.Hour/Minute/Secondfunc (t Time) Hour() intExtract time-of-day components
t.Weekdayfunc (t Time) Weekday() WeekdayReturn day of week (Sunday=0)
time.Secondconst Duration = 1000000000One second as Duration
time.Millisecondconst DurationOne millisecond as Duration
time.Minuteconst DurationOne minute as Duration
time.Hourconst DurationOne hour as Duration
time.RFC3339const layout stringISO 8601 / RFC 3339 time layout
time.RFC822const layout stringRFC 822 time layout
time.Kitchenconst layout = "3:04PM"Kitchen clock layout

💡 Example Program

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 →