在golang中解析大写字母月份

时间:2019-04-09 10:19:16

标签: date go time

我想将"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中用大写字母解析带有月份名称的日期字符串?

1 个答案:

答案 0 :(得分:1)

  

Package time

import "time" 
     

公认的月份格式为“一月”和“一月”。


解析布局的缩写月份格式为“ Jan”。使用“ 2006年1月2日”作为解析布局。

例如

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>