[Go] Format a time as a string

Created on November 30, 2026

Call Format on a time.Time value. In this example, time.Now() supplies the current time. Go uses a reference date to describe the layout. In this format, 2006 represents the year, 01 the month, 02 the day, 15 the hour in 24-hour time, 04 the minute, and 05 the second.

package main

import (
    "fmt"
    "time"
)

func main() {
    currentTime := time.Now()
    fmt.Println(currentTime.Format("2006-01-02 15:04:05"))
}

Example output

2021-07-18 23:40:16

See the time package layout constants for the reference values.

Go