How to parse unix timestamp to time.Time

后端 未结 6 2045
悲&欢浪女
悲&欢浪女 2020-12-02 05:27

I\'m trying to parse an Unix timestamp but I get out of range error. That doesn\'t really makes sense to me, because the layout is correct (as in the Go docs):



        
6条回答
  •  感动是毒
    2020-12-02 06:26

    Just use time.Parse

    example:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        fromString := "Wed, 6 Sep 2017 10:43:01 +0300"
        t, e := time.Parse("Mon, _2 Jan 2006 15:04:05 -0700", fromString)
        if e != nil {
            fmt.Printf("err: %s\n", e)
        }
        fmt.Printf("UTC time: %v\n", t.UTC())
    }
    

    Working example on play.golang.org.

提交回复
热议问题