duankan8739 2017-11-23 07:01
浏览 771
已采纳

将日期从DD-MON-YY转换为YYYYMMDD

In GoLang, how should I convert date from DD-MON-YY to YYYYMMDD format?

My input is: 9-Nov-17
Expected output is: 20171109

But when I do below:

t, err := time.Parse("20060102", "9-Nov-17")

Golang returns error:

Error: parsing time "9-Nov-17" as "20060102": cannot parse "v-17" as "2006"

Please help.

  • 写回答

2条回答 默认 最新

  • dongping2023 2017-11-23 07:09
    关注

    You have to parse the time in the current format and then format it into the expected format using Format function

    Check the answer below

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        // Parse a time value from a string in dd-Mon-yy format.
        t, _ := time.Parse("2-Jan-06", "9-Nov-17")
        //Format and print time in yyyymmdd format
        fmt.Println(t.Format("20060102"))
    }
    

    play link here : goplayground

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?