douweng1904
2017-05-11 12:48
浏览 2.3k
已采纳

如何在golang中将Unix时间转换为time.Time? [重复]

This question already has an answer here:

I want to covert Unix time (1494505756) to UTC format just

import "time"
timeNow := time.Now()

I want to restore timeNow to UTC format. How to do that?

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 在这里有答案

  • 如何将unix时间戳解析为时间。 6个答案

    我想将Unix时间(1494505756)转换为UTC格式 just < / p>

     导入“时间” 
    timeNow:= time.Now()
       
     
     

    我想将timeNow恢复为UTC 格式。 怎么做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongqiabei7682 2017-05-11 12:58
    已采纳

    You can get UTC and unix from time interface itself.

    To convert unix timestamp to time object. Use this:

    t := time.Unix(1494505756, 0)
    fmt.Println(t)
    

    func Unix(sec int64, nsec int64) Time

    Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).

    For UTC:

    time.Now().UTC()
    

    UTC returns t with the location set to UTC. Link: https://golang.org/pkg/time/#UTC

    For Unix:

    time.Now().Unix()
    

    Unix returns t as a Unix time, the number of seconds elapsed since January 1, 1970 UTC. Link:

    点赞 打赏 评论

相关推荐 更多相似问题