Cookies management by TermsFeed Cookie Consent

📅 YYYY-MM-DD date format in Go

Last updated:
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.

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!

⏲️ 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

❤️‍🩹 Recover function in Go

Learn what it is for and how to use the built-in recover() function
introduction syntax builtin