Cookies management by TermsFeed Cookie Consent

📅 YYYY-MM-DD date format in Go

Last updated:
introduction time

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.

See also the cheatsheet on date and time format in Go.

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

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

📝 Convert date or time to string in Go

shorts introduction time

✨ 5 different ways to loop over a time.Ticker in Go

Learn how to use the popular time.Ticker struct in loops
introduction time

⏲️ Measure execution time in Go

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