dongyuan8312 2017-10-31 14:10
浏览 231
已采纳

我如何在Golang中将RFC3339转换为UNIX

I want to show some RFC3339 time as seconds. I found how to parse times string, but it not that

t, _ := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00") 
  • 写回答

1条回答 默认 最新

  • doujiacai4986 2017-10-31 14:15
    关注

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
        if err != nil {
            fmt.Println(err)
            return
        }
        fmt.Println(t)
        // Unix returns t as a Unix time,
        // the number of seconds elapsed since January 1, 1970 UTC.
        fmt.Println(t.Unix())
    }
    

    Playground: https://play.golang.org/p/LG6G4lMIWt

    Output:

    2012-11-01 22:08:41 +0000 UTC
    1351807721
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?