【问题标题】:parse capital letter month in golang在golang中解析大写字母月份
【发布时间】:2019-04-09 10:19:15
【问题描述】:

我想将"25-APR-2019"之类的字符串解析为time.time

我知道使用解析日期

date, err := time.Parse(layout, stringVariableForDate)

但我在https://golang.org/pkg/time/#pkg-constants中没有找到布局选项

我不能使用 JAN,因为使用这个,我收到错误:

panic: parsing time "25-APR-2019" as "02-JAN-2006": cannot parse "APR-2019" as "-JAN-"

如何在 go-lang 中解析月份名称为大写字母的日期字符串?

【问题讨论】:

    标签: date go time


    【解决方案1】:

    Package time

    import "time" 
    

    可识别的月份格式为“Jan”和“January”。


    解析布局缩写月份格式为“Jan”。解析布局使用“02-Jan-2006”。

    例如,

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        date, err := time.Parse("02-Jan-2006", "25-APR-2019")
        fmt.Println(date, err)
    }
    

    游乐场:https://play.golang.org/p/5MRpUrUVJt4

    输出:

    2019-04-25 00:00:00 +0000 UTC <nil>
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多