Home  >  Article  >  Backend Development  >  Golang parses date in RFC822Z format without leading zeros

Golang parses date in RFC822Z format without leading zeros

王林
王林forward
2024-02-09 19:57:20927browse

Golang parses date in RFC822Z format without leading zeros

When parsing dates in RFC822Z format in Golang, problems may occur when encountering date formats without leading zeros. The RFC822Z format is a commonly used date format, but sometimes the month and day in the date do not have leading zeros. Special handling is required when parsing dates in this format, otherwise parsing errors may result. This article will introduce how to use Golang to parse date formats without leading zeros and provide corresponding code examples. By reading this article, you will learn how to correctly parse dates in RFC822Z format, and how to handle date formats without leading zeros.

Question content

I have an uncontrollable date string that I'm trying to parse into a date.

This format is most similar to rfc822z.

rfc822z="January 2, 2006 15:04 -0700"

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

However, it does not have leading zeros.

Example: "December 5, 2022 20:15:21 0000"

The approach I've seen in other posts is to write manual formatting.

parsetime, timeparseerror = time.parse("2 jan 2006 15:04:21 -0700", stringdate)

However, when I try to do this, I get the warning:

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

Not surprisingly, running it failed despite the warning.

Workaround

Your time format does not match - in your example you have "December 5, 2022" but you are using "2 Jan 06" and In your reference format you have "15:04:21" but it should be "15:04:05".

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

https://www.php.cn/link/efb53b87d5f5286f29b7791ec36a34f8

The above is the detailed content of Golang parses date in RFC822Z format without leading zeros. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete