dongzhanlu0658 2019-04-19 15:54
浏览 693

如何使用Go中的GMT以格式显示当前日期和时间?

I'm trying to get date and time like this 2014-06-22 22:00:22 GMT+2 What is the best way to do it?

I'm trying various variants. The closest was this

time.Now().Format("2006-01-02 15:04:05 GMT+1")

And I get good result, except the GMT - it's wrong. I'v triyed to change the template like this and got results:

GMT+0 => GMT+0

GMT+1 => GMT+4

But I live in GMT+3 and expect that number. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • doushui3061 2019-04-19 15:59
    关注

    Per the documentation the time package supports hhmm, hh:mm, and hh formats; single-digit h offset is not directly supported:

    Numeric time zone offsets format as follows:

    -0700  ±hhmm
    -07:00 ±hh:mm
    -07    ±hh
    

    Remember that the format string for time.Format is telling it how to render the parts of Time. It does not change anything about the Time, including its locale. If you want to render a time in a different time zone, use Time.In to get the time in a different time zone:

    timeInGMTPlus3 := time.Now().In(time.FixedZone("some zone", int((3 * time.Hour).Seconds()))
    
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路