英文:
How to parse date/time with dots/periods golang
问题
如何解析带有点/句号的日期/时间字符串 01.08.2022 17:00:02
package main
import "fmt"
import "time"
func main() {
date, err := time.Parse("2.Jan.2006 15:04:05", "01.08.2022 17:00:02")
if err != nil {
panic(err)
}
fmt.Println(date)
}
这会导致 panic: parsing time "01.08.2022 17:00:02" as "2.Jan.2006 15:04:05": cannot parse "08.2022 17:00:02" as "Jan"
英文:
How do I parse date/time string with dots/periods 01.08.2022 17:00:02
package main
import "fmt"
import "time"
func main() {
date, err := time.Parse("2.Jan.2006 15:04:05", "01.08.2022 17:00:02")
if err != nil {
panic(err)
}
fmt.Println(date)
}
This results in panic: parsing time "01.08.2022 17:00:02" as "2.Jan.2006 15:04:05": cannot parse "08.2022 17:00:02" as "Jan"
答案1
得分: 0
正确的格式是:
date, err := time.Parse("2.1.2006 15:04:05", "01.08.2022 17:00:02")
格式中的Jan
需要使用月份的全名而不是数字。
英文:
The correct format is:
date, err := time.Parse("2.1.2006 15:04:05", "01.08.2022 17:00:02")
The Jan
in format will require the month name spelled out instead of a number.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论