首页 > 解决方案 > 在golang中解析大写字母月份

问题描述

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

标签: dategotime

解决方案


打包时间

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>

推荐阅读