To format date in Go as YYYY-MM-DD
, use the time.Format()
operate with the format: "2006-01-02"
. To format date as DD/MM/YYYY
, use the "02/01/2006"
format. To format date as YYYY-MM-DD hh:mm:ss
, use the "2006-01-02 15:04:05"
format.
Examples
If you’re not conversant in the Go date formatting layouts, learn the documentation within the
time
package deal.
See additionally the cheatsheet on date and time format in Go.
YYYY-MM-DD date format in Go
package deal essential
import (
"fmt"
"time"
)
const (
YYYYMMDD = "2006-01-02"
)
func essential() {
now := time.Now().UTC()
fmt.Println(now.Format(YYYYMMDD))
}
Output:
DD/MM/YYYY date format in Go
package deal essential
import (
"fmt"
"time"
)
const (
DDMMYYYY = "02/01/2006"
)
func essential() {
now := time.Now().UTC()
fmt.Println(now.Format(DDMMYYYY))
}
Output:
YYYY-MM-DD hh:mm:ss date format in Go
package deal essential
import (
"fmt"
"time"
)
const (
DDMMYYYYhhmmss = "2006-01-02 15:04:05"
)
func essential() {
now := time.Now().UTC()
fmt.Println(now.Format(DDMMYYYYhhmmss))
}
Output: