tl;dr
- Is there a WAI-ARIA-compliant way to format dates according to language/country?
- My intention is to show dates (for instance the date a post was published on a blog) in a way that varies depending on factors such as the screen width, and the language of choice of the user (English vs other language); these are just two examples I can think of (I think I'd use a
@media (max-width: whatever)
CSS rule to address the former need, and target elements based on thelang
attribute for the latter).
I first thought how do I format a date according to one or another convention (e.g. british vs american, month as a number vs as a word, ...) using CSS? and I found this question where one of the answers suggests a way to use CSS to format dates, and one could build on that to provide classes specific to countries/formats, and then some inputs from the user (choose the country/language) plus some JavaScript could encode the logic to show dates in one format or another.
But that answer assumes that the parts of the date made different HTML elements, like in the following snippets:
<div class="date-wrapper">
<div class="date-year" val="1994"></div>
<div class="date-month" val="03"></div>
<div class="date-day" val="09"></div>
</div>
The above can be styled with a fairly articulated, long, and repetitive set of CSS rules. If it was just this, I would be ok with it. I'd put the CSS in a file, and forget about it.
But the point is, I think the above HTML snippet really is just a random blob of stuff to a screen reader, which would not present it as a date, but just as "a div
with 3 div
s each of which has a number as its val
attribute". Just blob.
So my next thought was before I even think of applying a style to a date, how do I even encode that a string like 1/12/2021
written somewhere in my HTML is a date? And so I found that there is a <time>
HTML element, and I found that it can't help me in this respect.