doucong8553 2019-04-09 10:19
浏览 151
已采纳

在golang中解析大写字母月份

I want to parse string like "25-APR-2019" to time.time

I know parsing date using

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

But I didn't find layout option in https://golang.org/pkg/time/#pkg-constants

I can not use JAN, as using this, I am getting error :

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

How can I parse date string with month name in capital letter in go-lang?

  • 写回答

1条回答 默认 最新

  • dongtingxiao4697 2019-04-09 10:22
    关注

    Package time

    import "time" 
    

    The recognized month formats are "Jan" and "January".


    The parse layout abbreviated month format is "Jan". Use "02-Jan-2006" for the parse layout.

    For example,

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

    Playground: https://play.golang.org/p/5MRpUrUVJt4

    Output:

    2019-04-25 00:00:00 +0000 UTC <nil>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序