在 GoLang 中,我应该如何将日期从 DD-MON-YY 格式转换为 YYYYMMDD 格式?
My input is: 9-Nov-17
Expected output is: 20171109
但是当我执行以下操作时:
t, err := time.Parse("20060102", "9-Nov-17")
Golang 返回错误:
Error: parsing time "9-Nov-17" as "20060102": cannot parse "v-17" as "2006"
请帮忙。
最佳答案
您必须解析当前格式的时间,然后使用Format
函数将其格式化为预期的格式
检查下面的答案
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"))
}
播放链接在这里:goplayground
关于date - 将日期从 DD-MON-YY 转换为 YYYYMMDD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47449410/