drpkcwwav20524605
2018-12-17 15:14
浏览 179
已采纳

在golang中解析日期

Please help me to parse date like 2018-12-17 12:55:50 +0300 in golang

I'm trying with layout

layout := "2006-01-02 15:04:05 +0000"
dateString := "2018-12-17 12:55:50 +0300"
t, err := time.Parse(layout, dateString)
if err != nil {
    fmt.Println("Error while parsing date :", err)
}
fmt.Println(t.Format("2006-01-02 15:04:05"))

but compiler says Error while parsing date : parsing time "2018-12-17 12:55:50 +0300" as "2006-01-02 15:04:05 +0000": cannot parse "300" as " +0000"

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

请帮助我解析日期,例如 2018-12-17 12:55:50 +0300

我正在尝试使用布局

  layout:=“ 2006-01-02 15:04:05 +  0000“ 
dateString:=” 2018-12-17 12:55:50 +0300“ 
t,err:= time.Parse(layout,dateString)
if err!= nil {
 fmt.Println(” 解析日期:“,err)
} 
fmt.Println(t.Format(” 2006-01-02 15:04:05“))
   
 
 

但编译器在解析日期时显示 Error:解析时间“ 2018-12-17 12:55:50 +0300”为“ 2006-01-02 15:04:05 +0000”:无法将“ 300”解析为“ +0000“

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dty47696 2018-12-17 15:18
    最佳回答

    Package time

    import "time"

    the [layout] reference time can be thought of as

    01/02 03:04:05PM '06 -0700
    

    Use -0700, not +0000.

    For example,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        layout := "2006-01-02 15:04:05 -0700"
        dateString := "2018-12-17 12:55:50 +0300"
        t, err := time.Parse(layout, dateString)
        if err != nil {
            fmt.Println("Error while parsing date :", err)
        }
        fmt.Println(t.Format("2006-01-02 15:04:05"))
    }
    

    Playground: https://play.golang.org/p/Xr8zaTjODgC

    Output:

    2018-12-17 12:55:50
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题