在Golang中格式化日期时间值

Go日期时间格式教程展示了如何在Golang中格式化日期时间值。

在Go中,我们使用time.Format函数来格式化日期时间值。

func (t Time) Format(layout string) string

该函数根据参数定义的布局返回时间值的文本表示形式

Go不使用格式说明符(例如yyyy-mm-dd)来格式化日期时间值的常用方法。相反,它使用MonJan215:04:05MST2006的uniqedatetime值。因此,为了格式化日期时间值,我们选择了这个固定日期时间的特定布局。

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 version
go version go1.18.1 linux/amd64

我们使用Go版本1.18。

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

转到预定义的日期时间格式

time包中,有几种预定义的时间格式。

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时间格式戳

还有预定义的时间戳格式。

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中的日期和时间。

列出所有Go教程。

赞(0) 打赏

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏