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

如何在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>
  • 写回答

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:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)