Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

📅 YYYY-MM-DD date format in Go

introduction time

Please consider supporting us by disabling your ad blocker

To format date in Go as YYYY-MM-DD, use the time.Format() function with the layout: "2006-01-02". To format date as DD/MM/YYYY, use the "02/01/2006" layout. To format date as YYYY-MM-DD hh:mm:ss, use the "2006-01-02 15:04:05" layout.

Examples

If you are not familiar with the Go date formatting layouts, read the documentation in the time package.

YYYY-MM-DD date format in Go

package main

import (
    "fmt"
    "time"
)

const (
    YYYYMMDD = "2006-01-02"
)

func main() {
    now := time.Now().UTC()
    fmt.Println(now.Format(YYYYMMDD))
}

Output:

2022-03-14

DD/MM/YYYY date format in Go

package main

import (
    "fmt"
    "time"
)

const (
    DDMMYYYY = "02/01/2006"
)

func main() {
    now := time.Now().UTC()
    fmt.Println(now.Format(DDMMYYYY))
}

Output:

14/03/2022

YYYY-MM-DD hh:mm:ss date format in Go

package main

import (
    "fmt"
    "time"
)

const (
    DDMMYYYYhhmmss = "2006-01-02 15:04:05"
)

func main() {
    now := time.Now().UTC()
    fmt.Println(now.Format(DDMMYYYYhhmmss))
}

Output:

2022-03-14 05:41:33

⏲️ Measure execution time in Go

Learn how to measure the time taken by a function
introduction time

😴 Sleep function in Go - pause the program execution

Learn how to pause the execution of a current Goroutine
introduction time

🎲 Generate a random string in Go

Learn how to generate a random string of a fixed length
introduction random strings