How to parse unix timestamp to time.Time

后端 未结 6 2049
悲&欢浪女
悲&欢浪女 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:28

    You can directly use time.Unix function of time which converts the unix time stamp to UTC

    package main
    
    import (
      "fmt"
      "time"
    )
    
    
    func main() {
        unixTimeUTC:=time.Unix(1405544146, 0) //gives unix time stamp in utc 
    
        unitTimeInRFC3339 :=unixTimeUTC.Format(time.RFC3339) // converts utc time to RFC3339 format
    
        fmt.Println("unix time stamp in UTC :--->",unixTimeUTC)
        fmt.Println("unix time stamp in unitTimeInRFC3339 format :->",unitTimeInRFC3339)
    }
    

    Output

    unix time stamp in UTC :---> 2014-07-16 20:55:46 +0000 UTC
    unix time stamp in unitTimeInRFC3339 format :----> 2014-07-16T20:55:46Z
    

    Check in Go Playground: https://play.golang.org/p/5FtRdnkxAd

提交回复
热议问题