Go 日期时间格式
最后修改时间 2024 年 4 月 11 日
在本文中,我们展示如何在 Golang 中格式化日期时间值。
在 Go 中,我们使用 time.Format
函数来格式化日期时间值。
func (t Time) Format(layout string) string
该函数根据参数定义的布局返回时间值的文本表示。
Go 不使用像 yyyy-mm-dd
这样的格式说明符来格式化日期时间值。而是使用一个唯一的日期时间值 Mon Jan 2 15:04:05 MST 2006
。因此,为了格式化日期时间值,我们选择这个固定日期时间的特定布局。
const ( Layout = "01/02 03:04:05PM '06 -0700" ANSIC = "Mon Jan _2 15:04:05 2006" UnixDate = "Mon Jan _2 15:04:05 MST 2006" RubyDate = "Mon Jan 02 15:04:05 -0700 2006" RFC822 = "02 Jan 06 15:04 MST" RFC822Z = "02 Jan 06 15:04 -0700" RFC850 = "Monday, 02-Jan-06 15:04:05 MST" RFC1123 = "Mon, 02 Jan 2006 15:04:05 MST" RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700" RFC3339 = "2006-01-02T15:04:05Z07:00" RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00" Kitchen = "3:04PM" // Handy time stamps. Stamp = "Jan _2 15:04:05" StampMilli = "Jan _2 15:04:05.000" StampMicro = "Jan _2 15:04:05.000000" StampNano = "Jan _2 15:04:05.000000000" )
time
模块中有几种预定义的布局可用。
Go 日期时间格式示例
在第一个示例中,我们格式化当前日期时间。
main.go
package main import ( "fmt" "time" ) func main() { now := time.Now() fmt.Println("Time: ", now.Format("15:04:05")) fmt.Println("Short date:", now.Format("Jan 2, 2006")) fmt.Println("Long date:", now.Format("Mon Jan 2 15:04:05 2006")) }
我们使用 time.Now
函数获取当前日期时间,并利用 Format
函数以三种方式格式化它。
$ go run main.go Time: 18:10:34 Short date: May 29, 2022 Long date: Sun May 29 18:10:34 2022
Go 预定义的日期时间格式
在 time
包中,有几种预定义的时间格式。
main.go
package main import ( "fmt" "time" ) func main() { now := time.Now() fmt.Println("ANSIC:", now.Format(time.ANSIC)) fmt.Println("Layout:", now.Format(time.Layout)) fmt.Println("RFC1123:", now.Format(time.RFC1123)) fmt.Println("RFC1123Z:", now.Format(time.RFC1123Z)) fmt.Println("Kitchen:", now.Format(time.Kitchen)) fmt.Println("RFC3339:", now.Format(time.RFC3339)) fmt.Println("RFC3339Nano:", now.Format(time.RFC3339Nano)) fmt.Println("RFC822:", now.Format(time.RFC822)) fmt.Println("RFC822Z:", now.Format(time.RFC822Z)) fmt.Println("RFC850:", now.Format(time.RFC850)) fmt.Println("RubyDate:", now.Format(time.RubyDate)) fmt.Println("UnixDate:", now.Format(time.UnixDate)) fmt.Println("RFC1123:", now.Format(time.RFC1123)) fmt.Println("RFC1123Z:", now.Format(time.RFC1123Z)) }
该示例使用了十四种预定义格式。
$ go run main.go ANSIC: Sun May 29 18:15:38 2022 Layout: 05/29 06:15:38PM '22 +0200 RFC1123: Sun, 29 May 2022 18:15:38 CEST RFC1123Z: Sun, 29 May 2022 18:15:38 +0200 Kitchen: 6:15PM RFC3339: 2022-05-29T18:15:38+02:00 RFC3339Nano: 2022-05-29T18:15:38.753172438+02:00 RFC822: 29 May 22 18:15 CEST RFC822Z: 29 May 22 18:15 +0200 RFC850: Sunday, 29-May-22 18:15:38 CEST RubyDate: Sun May 29 18:15:38 +0200 2022 UnixDate: Sun May 29 18:15:38 CEST 2022 RFC1123: Sun, 29 May 2022 18:15:38 CEST RFC1123Z: Sun, 29 May 2022 18:15:38 +0200
Go 时间格式戳
还有预定义的时间戳格式。
main.go
package main import ( "fmt" "time" ) func main() { now := time.Now() fmt.Println("Stamp:", now.Format(time.Stamp)) fmt.Println("StampMicro:", now.Format(time.StampMicro)) fmt.Println("StampMilli:", now.Format(time.StampMilli)) fmt.Println("StampNano:", now.Format(time.StampNano)) }
该示例以四种可用的时间戳打印当前时间。
$ go run main.go Stamp: May 29 18:20:00 StampMicro: May 29 18:20:00.571331 StampMilli: May 29 18:20:00.571 StampNano: May 29 18:20:00.571331153
来源
在本文中,我们研究了 Go 中的日期和时间。