欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

go 时间和日期 格式化

程序员文章站 2022-07-15 09:36:52
...

 

    t:=time.Now()

    fmt.Println(time.Now())
    fmt.Printf("%4d %02d %02d\n",t.Year(),t.Month(),t.Day())
    fmt.Printf("%02d.%02d.%4d\n", t.Day(), t.Month(), t.Year())

    t = time.Now().UTC()
    fmt.Println(t)

    fmt.Println(t.Format(time.RFC822)) // 21 Dec 11 0852 UTC
    fmt.Println(t.Format(time.ANSIC)) // Wed Dec 21 08:56:34 2011
    fmt.Println(t.Format("02 Jan 2006 15:04")) // 21 Dec 2011 08:52
    s := t.Format("20060102")
    fmt.Println(t, "=>", s)

    s = t.Format("20060102 15:04:05")
    fmt.Println(t, "=>", s)

输出:

2020-05-21 10:57:26.208685 +0800 CST m=+0.000072362
2020 05 21
21.05.2020
2020-05-21 02:57:26.20884 +0000 UTC
21 May 20 02:57 UTC
Thu May 21 02:57:26 2020
21 May 2020 02:57
2020-05-21 02:57:26.20884 +0000 UTC => 20200521
2020-05-21 02:57:26.20884 +0000 UTC => 20200521 02:57:26

 

相关标签: go go time