60 points · by patelpankaj · 6y ago · 57 comments
You can use Date.prototype.toLocaleString and its customizable API to get the localized output easily. Like in the following example, we will use the Locale String to easily get the Readable Date desired by the user
Actual content: "... you might not need any of these libraries [moment, dayjs, date-fns] to have some basic Formatting and Localization on the Date Objects."
You haven't though because there isn't a locale setting in the browser.
Lets get rid of all date-time formats except the iso(YYY-MM-DD) one...
If a user sees some random date, such as 3/4/xxxx, they have absolutely no way to distinguish if this is in their format or not.
If you're in the US, you might assume that this is in your backwards M/D/Y format, meaning it's April 3rd, and you'd be right much of the time.
However, if you're outside the US, then there are two indistinguishable possibilities:
It's a US server ignoring your time format, so this date represents April 3rd.
OR
It's a server correctly showing you a date in your preferred format, so it's actually the 4th of March.
At a glance, these aren't distinguishable. I easily click through 200-500 pages a day! I'm not going to sit down and try to work out which of those hundreds of pages a day, and thousands of pages a week are designed for humans, or just Americans.
If you're in the US, you might assume that this is in your backwards M/D/Y format, meaning it's April 3rd, and you'd be right much of the time.
If I, in the US, see 3/4/xxxx, its meaning is March 4th, definitely not April 3rd.
But the fact that you got your example wrong only proves your overall point that it's a mess!
[1]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ti...
Also, I don't believe you can rely on a browser having a particular locale available. I'm not sure how reliable time zone data will be either, if you need to show something in a different time zone.
I think for some general use-cases this will work well. But there's still a lot of weird issues around this stuff in the browser.
That would all be possible without third party libraries, but often you want to subtract 2 weeks from a date and that is just a pain in the arse with timestamps. You also might want to import/export dates to databases, which can be picky about the format.
I am no web developer, so others might have better opinions. And I think the author is correct that people not used to certain libraries should try the standard ones first.
Yes, obviously you can forego DRY and write and maintain a bunch of boilerplate for date manipulation, or write and maintain your own date library, but for most uses itmakes a lot more sense to use an existing library. For new uses, probably not momentjs, though—probably date-fns, or luxon, or...
Recently learned that you can internationalize dates right in your Postgres queries/functions! tps://stackoverflow.com/a/59632278/741970. Postgres has great date functionality in general, but with Linux locales installed you can use it for just about anything date related.
So yes, you do.
I wound up switching to Luxon because of this. Which is too bad, because date-fns is a fine library otherwise.
People like reading nice dates and nice words. They're easy to parse for human brains. Sure, "1e6" is nice and short but typing out "a million" is much easier to understand. Giving someone a notification that they have an appointment "next Thursday (2020-02-27)" is a lot clearer than just stating the date. It saves people from having to look up the current date and doing mental math to find out how much time is in between those dates.
Nit everyone is a programmer and not everyone likes short and concise data notation. Date localisation exists for a reason and it's not because people like to make developers' lives impossible.
Use ISO8601 because of its technical superiority if you want, but don't think it's the final answer to the date/time problem.
How is 2020-02-27not nice?
> Sure, "1e6" is nice and short but typing out "a million" is much easier to understand
IMHO 1 000 000 is the easiest (sure the 1e6 from is better for bigger numbers). As for "a million" I don't understand anything bigger described this way (billions, trillions etc) - tell me 1 trillion and 2 billions and I have absolutely no idea how much is that. It wouldn't be a problem for me to memorize unless there were not the long and the short system as I rarely have a clue which one is being used.
> "next Thursday (2020-02-27)" is a lot clearer than just stating the date
Sure, but something like 02.27,2020 is no way better than 2020-02-27, it's just a matter of habit particularly easy to change.
Habits are not easy to change and changing them gets harder as you get older. Microsoft changing the icon of the start menu got me many questions from people anywhere between 20 and 80 back when I did tech support. These people were smart enough to use a computer yet a tiny change confused them. When Windows changed their start menu to something new, people didn't want to change the way they did things and tools like Classic Start was born. If clicking an icon on a computer is too hard a habit to change, how do you think telling the world to change their date notation will go down?
And why should we all go do stuff differently because someone has come up with a way that makes lexicographical sorting of dates possible?
Furthermore, "next Thursday" is already part of internationalisation code (Thursday needs to be translated and the concept of what the literal translation of "next Thursday" means changes between languages and cultures). Why not also put the full date into the correct locale as well while you're at it?
Just because I think metric measurements are superior to imperial measurements doesn't mean I don't need to translate those units of measurement to American when I publish an app that uses them. Anyone capable of counting to ten can understand the metric system but that doesn't mean I can get away with providing metric-only information and input fields for everything.
There's a difference between what academics and engineers use and what the general public uses. If you don't make a general product for the general public then you don't make a very good product.
You mean the great majority of them, including official math notation?