2 回答

TA贡献1812条经验 获得超5个赞
这么晚了,但我想提出正则表达式之外的另一种解决方案。您可以使用 time 包来验证您的值,然后,如果需要,您可以稍后使用结果值。
package main
import (
"fmt"
"time"
)
func main() {
Form := "3:04pm"
timesToCheck := []string{"7:54pm", "09:00pm", "09:00am", "08:55pm", "08:54am", "1:00pm", "23:00am"}
for _, v := range timesToCheck {
if t, e := time.Parse(Form, v); e != nil {
fmt.Println(e.Error())
} else {
fmt.Printf("%#v\n", t)
}
}
}
这将为您提供以下信息:
time.Time{sec:-31550760, nsec:0, loc:(*time.Location)(0x1811e0)}
time.Time{sec:-31546800, nsec:0, loc:(*time.Location)(0x1811e0)}
time.Time{sec:-31590000, nsec:0, loc:(*time.Location)(0x1811e0)}
time.Time{sec:-31547100, nsec:0, loc:(*time.Location)(0x1811e0)}
time.Time{sec:-31590360, nsec:0, loc:(*time.Location)(0x1811e0)}
time.Time{sec:-31575600, nsec:0, loc:(*time.Location)(0x1811e0)}
parsing time "23:00am": hour out of range
- 2 回答
- 0 关注
- 320 浏览
添加回答
举报