將字符串時間轉換為go go go go time struction
當處理特定格式的字符串時間值時,會出現一個常見問題,該格式無法識別默認時間。要解析此字符串,我們可以根據其特定格式定義自己的佈局字符串。
YYYY: Four-digit year
MM: Two-digit month
DD: Two-digit day
T: Separator of date and timeHH: Two-digit hour
mm: Two-digit分鐘
SS:二維第二
- 在字符串
定義後,我們可以將其與time.parse(Parse()一起使用,以將字符串時間值轉換為time.parse time time time time time time time time stime stime.prime.press ntime shupter.press(&&&&&&&&uperstuct.parse()。 “ 20171023T183552”
佈局:=“ 20060102T150405”
t,err:= time.parse(佈局,s)
如果err! = nil {
fmt.println(err)
}- Output:
- 2017-10-23 18:35:52 0000 UTC
- Conclusion
- By customizing the layout string, we can successfully parse string time values into time.Time structures in Go, even if their format differs從時代軟件包提供的標準選項。