Surprised by inflexibility of time.Parse wrt 1 or 2 digit months

206 views
Skip to first unread message

David Karr

unread,
Jun 9, 2025, 2:59:44 PMJun 9
to golang-nuts
I've used "time.Parse()" several times in various small applications, and today I realized a curious inflexibility with it.  If I have to parse date values where the months or days could be either 1 or 2 digits, which is all cases if the code might be dealing with dates anywhere in the year, then I have no choice but to implement a "normalize" function that adds a "0" at the start if the value is not two digits. I got this to work, but anyone who uses this method would have to write the same code. It seems odd that this wouldn't be handled automatically.

burak serdar

unread,
Jun 9, 2025, 3:05:26 PMJun 9
to David Karr, golang-nuts


On Mon, Jun 9, 2025, 10:00 PM David Karr <davidmic...@gmail.com> wrote:
I've used "time.Parse()" several times in various small applications, and today I realized a curious inflexibility with it.  If I have to parse date values where the months or days could be either 1 or 2 digits, which is all cases if the code might be dealing with dates anywhere in the year, then I have no choice but to implement a "normalize" function that adds a "0" at the start if the value is not two digits. I got this to work, but anyone who uses this method would have to write the same code. It seems odd that this wouldn't be handled automatically.


Did you try single digit for day and month in the format?

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/5f201757-f548-4374-958b-034b5c15473dn%40googlegroups.com.

David Karr

unread,
Jun 9, 2025, 3:15:52 PMJun 9
to golang-nuts
I did. It fails to parse dates with two digits for the month or day.

burak serdar

unread,
Jun 9, 2025, 3:22:53 PMJun 9
to David Karr, golang-nuts
You are right. I thought this worked.

Brian Candler

unread,
Jun 9, 2025, 3:27:09 PMJun 9
to golang-nuts
On Monday, 9 June 2025 at 20:15:52 UTC+1 David Karr wrote:
I did. It fails to parse dates with two digits for the month or day.

Can you show a specific example?? It works for me:

Message has been deleted

David Karr

unread,
Jun 10, 2025, 12:37:50 PMJun 10
to golang-nuts
Ok, well, after I played with it some more, I realized that a subfield format value of "1" will handle "1", "01", and "11". It's not necessary to use "01" or even "_1".

Sorry for the confusion.

Reply all
Reply to author
Forward
0 new messages