The date components are all magic numbers. Instead of how most programming languages use things like “yy,” “yyyy,” “MM”, “MMM”, etc., Go decided to use magic numbers for the date format. To get a 2 digit year format in go, use “06,” to get a 4 digit year, use “2006.” To get the hours as a 12-hour time, use “03” or “3.” To make it a 24-hr time, use “15,” etc.
I suspect it was supposed to be easier than remembering “is mm months and MM minutes or is it the other way around?” Everyone seems to get confused, though (myself included!), so I think they didn’t quite get it right.
Aha! Thanks very much for the prompt response. But I’m still confused.
Using the year as an example, what is the difference between 2006 and 2007? They are both four digit numbers, so I would expect a four digit year as the result. Why do I get 5 digits? I don’t understand how I go from 2021 using 2006 as the format, to 23007 by using 2007. Is 2006 the magic number (rather than just 4 digits)?
Because 2007 is not one of the magic numbers. 2 is a magic number for the day so you get “23” in the place of the “2” and then, I suppose, the formatter doesn’t know what to do with “007,” so it just leaves it.
Thanks for the hint. I may try it. I use third party packages where necessary.
I’m going to write something that is perhaps controversial here, which you refer to yourself: if I need to use a party package for ‘developer-friendly’ date and time functions, then the built-in functions are by definition ‘developer-unfriendly’. I took on board what skillian wrote about the confusion between MM and mm - I’ve done this myself quite a few times over the years, realised after that I was using month instead of minute and vice versa, and corrected it. It’s easy to get wrong and equally easy to correct. It would never occur to me that 2007 is so different to 2006, without knowing about the magic numbers being used.
I’m not what you would call a serious developer, I merely dabble. I have found Go very easy to get into, without any serious problems, just those typical with learning something new. This is the first time I’ve encountered something which I would describe as ‘weird’, and 'why would they do that?.
Yeah - this is one of the few things in go that irks me. MM vs mm seems easier than totally random numbers, and this question comes up a lot. Also, in terms of debugging, it’s easier for me to use MM and get months and then say “oh right; it’s mm” and change it than it is to have to go look up a list of magic numbers.