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