在GoLang,我应该如何将日期从DD-MON-YY转换为YYYYMMDD格式?
My input is: 9-Nov-17
Expected output is: 20171109
Run Code Online (Sandbox Code Playgroud)
但是当我在下面做的时候:
t, err := time.Parse("20060102", "9-Nov-17")
Run Code Online (Sandbox Code Playgroud)
Golang返回错误:
Error: parsing time "9-Nov-17" as "20060102": cannot parse "v-17" as "2006"
Run Code Online (Sandbox Code Playgroud)
请帮忙.
您必须以当前格式解析时间,然后使用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"))
}
Run Code Online (Sandbox Code Playgroud)
在这里播放链接:goplayground
| 归档时间: |
|
| 查看次数: |
463 次 |
| 最近记录: |