Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Time parse error in Golang (parse string to date format)

I am trying to parse the time from a string in Go lang, but it shows the error shown below. Can someone please explain what I’m doing wrong?

Code snippet:

date := "2023-02-20T17:16:51.535133Z"
fmt.Println("date ", date)
date_parsed, err := time.Parse("2023-02-20T17:16:51.535133Z", date)
fmt.Println("date_parsed ", date_parsed)
if err != nil {
        fmt.Println(err)
        return
    }

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

date  2023-02-20T17:16:51.535133Z
date_parsed 0001-01-01 00:00:00 +0000 UTC
parsing time "2023-02-20T17:16:51.535133Z" as "2023-02-20T17:16:51.535133Z": cannot parse "-02-20T17:16:51.535133Z" as "3"

>Solution :

Elements of the layout must use the documented reference times to be recognizable.

Each element shows by example the formatting of an element of the reference time. Only these values are recognized.

Year: "2006" "06"
Month: "Jan" "January" "01" "1"
Day of the week: "Mon" "Monday"
Day of the month: "2" "_2" "02"
Day of the year: "__2" "002"
Hour: "15" "3" "03" (PM or AM)
Minute: "4" "04"
Second: "5" "05"
AM/PM mark: "PM"

In other words:

  1. Month
  2. Day of month, day of year.
  3. Hour
  4. Minute
  5. Second
  6. Year
  7. Time zone

And fractions of a second are 9’s.

Your layout is 2006-01-02T15:04:05.999999Z. You can also use the existing time.RFC3339Nano.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading