douye1940 2016-08-31 23:43
浏览 21
已采纳

您如何以以下格式解析日期:

It's not clear to me from the documentation how I would parse a date in this somewhat strange format. It seems like it might not be possible.

2016-07-08T08:34:24+00:00

The following does not work (go play link)

package main

import (
    "fmt"
    "time"
)

func main() {
    date := "2016-07-08T08:34:24+00:00"
    d, err := time.Parse("2006-01-02T15:04:05+07:00", date)
    if err == nil {
        fmt.Println(d)
    } else {
        fmt.Println(err)
    }
}

Obviously a regexp could first check for this format and transform the + to a -, but that implies the standard library cant parse this date.

  • 写回答

1条回答 默认 最新

  • drb88830 2016-08-31 23:48
    关注

    Go's reference layout uses -7 hours as the timezone offset, but you used +7 hours:

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        date := "2016-07-08T08:34:24+00:00"
        d, err := time.Parse("2006-01-02T15:04:05-07:00", date)
        if err == nil {
            fmt.Println(d)
        } else {
            fmt.Println(err)
        }
    }
    

    https://play.golang.org/p/FNzx57R2jy

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题