doujia1988 2015-06-25 03:28
浏览 104
已采纳

如何在Go中的时区之间转换? [重复]

This question already has an answer here:

For example time.Now() has a timezone of UTC.

utcNow := time.Now()
fmt.Println(utcNow)

Outputs

2009-11-10 23:00:00 +0000 UTC

How do I convert this time to Japan Standard time?

</div>
  • 写回答

1条回答 默认 最新

  • dongzhu6900 2015-06-25 03:46
    关注

    It looks like you're running that in the Go playground, which is why the time is automatically set to UTC (it's also always set to November 2009 when a program is launched).

    If you run time.Now() on your own machine, it should pick up the local region. Alternatively, if you want to force the time to be in a specific timezone, you can use a time.Location object along with the time.Time.In function.

    l, err := time.LoadLocation("Asia/Tokyo") // Look up a location by it's IANA name.
    if err != nil {
        panic(err) // You can handle this gracefully.
    }
    fmt.Println(utcNow.In(l))
    

    Note that it's still showing the same moment in time, but now with JST's offset.

    For more information, look at the go documentation for the time package. http://golang.org/pkg/time

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

报告相同问题?

悬赏问题

  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器
  • ¥15 关于#爬虫#的问题:请问HMDB代谢物爬虫的那个工具可以提供一下吗
  • ¥15 vue3+electron打包获取本地视频属性,文件夹里面有ffprobe.exe 文件还会报错这是什么原因呢?
  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致