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

在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"

  • 写回答

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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Django的安装调试运行
  • ¥15 带序列特征的多输出预测模型
  • ¥15 VB.NET读取电脑主板序列号
  • ¥15 Python 如何安装 distutils模块
  • ¥15 关于#网络#的问题:网络是从楼上引一根网线下来,接了2台傻瓜交换机,也更换了ip还是不行
  • ¥15 资源泄露软件闪退怎么解决?
  • ¥15 CCF-CSP 2023 第三题 解压缩(50%)
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取