dongxieyou3314 2014-02-25 08:15
浏览 60
已采纳

如何正确使用time.Parse从Unix时间字符串创建时间对象?

We are trying to parse a unix timestamp, which is available as a string, into a time object, however, the following does not work:

package main

import (
"fmt"
"time"
)

func main() {
    t, _ := time.Parse(time.UnixDate, "1393344464")
    fmt.Printf("%v", t)
}

It keeps returning 0001-01-01 00:00:00 +0000 UTC. Go Playground.

  • 写回答

2条回答 默认 最新

  • dongtan6336 2014-02-25 08:32
    关注

    First of all, you have an error and you're not checking it: http://play.golang.org/p/7ruFfv5QHT which is bad practice (these errors are helpful for debugging! :) use them!)

    UnixDate is for unix string representations of dates; not timestamps. From the source: UnixDate = "Mon Jan _2 15:04:05 MST 2006".

    Use time.Unix:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        t := time.Unix(1393324260, 0)
        fmt.Printf("%v", t)
    }
    

    http://play.golang.org/p/gj_4EtiOVY

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部