Why is date formatting so hard in vanilla JS?
Editor's note: this was written before the new, long-overdue
Temporal
API was announced. Better days are almost here!
Suppose you want to deal with some dates while building a website. You don't want to roll
your own input parsing, so you use new Date(inputDateString)
; and you want to output
datestrings in a format that's clear, readable[^1], and (while we're making a wish list)
sorts the same both lexically and chronologically. That is: you want YYYY-MM-DD
. Surely
that is built in, right?
wrong
Oh. Wait, really? Ugh, fine.