— 1 min read
When was the last time you needed to display a formatted date somewhere in your applications? Since I work a lot on React (or generally JS) apps these days, I recently had the "pleasure" to format dates in JS. After receiving them from a Ruby API. Which in turn takes the (Postgres) db timestamps and converts them into Ruby (date)time objects. Oh the fun we had. "Of course" standardizations saves your ass in this situation. Usually at least.
What I did not run into (because of my luck working with Ruby and JS) is the nice little thing, Peter Hosey describes in his article "Unicode date formats, YYYY?!". You should click that link, if only just to see the url of the article. It's not the title I've mentioned above…
Short summary: There is this thing called "year for week-of-year". You probably know that sometimes at the end of the year the calendar week 1 already started—but we're still in the old year. If you print a date of that range, say December 31st 2018, you have to pay attention how your date-formatter-function actually formats the date. If you use the wrong pattern, you could end up with the wrong year!
Go ahead and click the link "Unicode date formats, YYYY?!" 😉
PS: If you want to display the year for week-of-year in Ruby/Rails, you'll need to use the pattern
1ISO 8601 week-based year and week number:2The week 1 of YYYY starts with a Monday and includes YYYY-01-04.3The days in the year before the first week are in the last week of4the previous year.5 %G - The week-based year6 %g - The last 2 digits of the week-based year (00..99)7 %V - Week number of the week-based year (01..53)