Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. Go Dictionary
  3. time.Now() / Sleep() / Format()

time.Now() / Sleep() / Format()

Go's time package lets you get the current time, pause program execution, and format timestamps. Use time.Now() to get the current time, and time.Sleep() to pause execution for a specified duration.

Syntax

import "time"

// Gets the current local time.
t := time.Now()

// Pauses execution for the specified duration.
time.Sleep(2 * time.Second)
time.Sleep(500 * time.Millisecond)

// Formats the time as a string using the specified layout.
s := t.Format("2006-01-02 15:04:05")  // Uses Go's reference time.

// Parses a time value from a string.
t2, err := time.Parse("2006-01-02", "2026-03-05")

// Adds or subtracts a duration from a time value.
tomorrow := t.Add(24 * time.Hour)
yesterday := t.Add(-24 * time.Hour)

Functions and Methods

Function/MethodDescription
time.Now()Returns the current local time.
time.Sleep(d)Pauses the current goroutine for the specified duration.
t.Format(layout)Returns the time formatted as a string using the specified layout.
time.Parse(layout, s)Parses a string according to the layout and returns a Time value.
t.Add(d)Returns a new Time value with the given duration added.
t.Sub(u)Returns the duration between two Time values.
t.Before(u) / t.After(u)Compares two time values and returns a bool.
t.Year() / Month() / Day()Returns the year, month, or day of the time value.
t.Hour() / Minute() / Second()Returns the hour, minute, or second of the time value.
t.Unix()Returns the Unix timestamp in seconds.
time.Since(t)Returns the elapsed time since t as a Duration.

Sample Code

package main

import (
    "fmt"
    "time"
)

func main() {
    // Get the current time and extract individual fields.
    now := time.Now()
    fmt.Println("Current time:", now)
    fmt.Printf("Year=%d Month=%d Day=%d\n", now.Year(), now.Month(), now.Day())
    fmt.Printf("Hour=%d Minute=%d Second=%d\n", now.Hour(), now.Minute(), now.Second())
    fmt.Println("Unix timestamp:", now.Unix())

    fmt.Println()

    // Format the time using Go's reference time: "2006-01-02 15:04:05".
    fmt.Println("yyyy/MM/dd format:", now.Format("2006/01/02"))
    fmt.Println("Date and time:", now.Format("2006-01-02 15:04:05"))
    fmt.Println("Long format:", now.Format("January 2, 2006 15:04"))
    fmt.Println("RFC3339 format:", now.Format(time.RFC3339))

    fmt.Println()

    // Add and subtract durations.
    tomorrow := now.Add(24 * time.Hour)
    fmt.Println("Tomorrow:", tomorrow.Format("2006-01-02"))

    nextWeek := now.Add(7 * 24 * time.Hour)
    fmt.Println("Next week:", nextWeek.Format("2006-01-02"))

    fmt.Println()

    // Measure elapsed time.
    start := time.Now()
    time.Sleep(100 * time.Millisecond) // Wait 100ms.
    elapsed := time.Since(start)
    fmt.Println("Elapsed:", elapsed)

    fmt.Println()

    // Parse a time value from a string.
    t, err := time.Parse("2006-01-02", "2026-03-05")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("Parsed date:", t.Format("January 2, 2006"))

    // Calculate the difference between two time values.
    diff := tomorrow.Sub(now)
    fmt.Printf("Hours from now to tomorrow: %.0f\n", diff.Hours())
}

Notes

Go's time formatting uses a unique reference time — January 2, 2006 at 15:04:05 — instead of placeholders like %Y-%m-%d used in other languages. You write your desired format by substituting that exact reference date and time. For example, "2006/01/02" produces output in YYYY/MM/DD format.

time.Duration represents a length of time. You combine constants like time.Second and time.Millisecond with multiplication. For example, two seconds is written as 2 * time.Second.

time.Sleep() blocks the current goroutine. In concurrent contexts such as servers, consider using time.After or time.Ticker instead.

If you find any errors or copyright issues, please .