如何在Golang中解析带有点/句号的日期/时间

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年9月4日 05:10:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/73595425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定