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

Golang parsing date in RFC822Z format without leading zero

I have a date string I can’t control that I’m trying to parse into a Date.

The format most closely resembles RFC822Z.

RFC822Z = "02 Jan 06 15:04 -0700"

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

Reference: https://yourbasic.org/golang/format-parse-string-time-date-example/

However, it does not have the leading zero.

Example: "5 Dec 2022 20:15:21 +0000"

The way I saw in other posts, is to write a manual format.

parseTime, timeParseError = time.Parse("2 Jan 2006 15:04:21 -0700", stringDate)

However, when I try that, I get a warning:

parsing time "2 Jan 2006 15:04:21 -0700" as "2 Jan 2006 15:04:21 -0700": cannot parse " -0700" as "1" (SA1002)

Running it despite the warning fails to part, unsurprisingly.

>Solution :

Your time format doesn’t match – in your example you have "5 Dec 2022", but you are using "2 Jan 06", and in your reference format you hvae "15:04:21" but it should be "15:04:05".

Your reference format should be 2 Jan 2006 15:04:05 -0700 not 2 Jan 06 15:04:21 -0700

https://go.dev/play/p/Shc381WgB6_7

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