Get current time as formatted string in Go?

后端 未结 8 525
北荒
北荒 2021-01-30 01:15

What\'s the best way to get the current timestamp in Go and convert to string? I need both date and time in eg. YYYYMMDDhhmmss format.

8条回答
  •  北海茫月
    2021-01-30 01:50

    Use the time.Now() and time.Format() functions (as time.LocalTime() doesn't exist anymore as of Go 1.0.3)

    t := time.Now()
    fmt.Println(t.Format("20060102150405"))
    

    Online demo (with date fixed in the past in the playground, never mind)

提交回复
热议问题