I’ll be the first to admit that I’m writing this article, in part, because it’s something I look up often and want to be able to find it next time. Formatting a date string that you get from an API in JavaScript can take many shapes ā anything from loading all of Moment.js to have very finite control, or just using a couple of lines to update it. This article is not meant to be comprehensive, but aims to show the most common path of human legibility.
ISO 8601 is an extremely common date format. The āZā at the end means the time in ISO 8601 format is using the UTC standard, i.e. no timezone. Here’s an example: 2020-05-25T04:00:00Z
. When I bring data in from an API, it’s typically in ISO 8601 format.
If I wanted to format the above string in a more readable format, say May 25, 2020, I would do this:
const dateString = '2020-05-14T04:00:00Z'
const formatDate = (dateString) => {
const options = { year: "numeric", month: "long", day: "numeric" }
return new Date(dateString).toLocaleDateString(undefined, options)
}
Here’s what I’m doing…
First, I’m passing in options for how I want the output to be formatted. There are many, many other options we could pass in there to format the date in different ways. I’m just showing a fairly common example.
const options = { year: "numeric", month: "long", day: "numeric" }
Next, I’m creating a new Date
instance that represents a single moment in time in a platform-independent format.
return new Date(dateString)
Finally, I’m using the .toLocaleDateString()
method to apply the formatting options.
return new Date(dateString).toLocaleDateString(undefined, options)
Note that I passed in undefined
. Not defining the value in this case means the time will be represented by whatever the default locale is. You can also set it to be a certain area/language. Or, for apps and sites that are internationalized, you can pass in what the user has selected (e.g. 'en-US'
for the United States, 'de-DE'
for Germany, and so forth). There’s a nice npm package that includes list of more locales and their codes.
Hope that helps you get started! And high fives to future Sarah for not having to look this up again in multiple places. 🤚
How did you pass dateString? Or am I missing something?
Pass the
dateString
variable as a parameter to theformatDate
function:formatDate(dateString)
UTC is not a format, it’s a standard for time. The format you are referring to is called ISO 8601. The “Z” at the end means the time in ISO 8601 format is using the UTC standard, i.e. no timezone.
My mistake, thank you! Updated.
Great article! Nice and concise.
Since Intl has become available in JS I have been using it more. Lots of control, especially good to easily change a multitude of options if you have international needs.
MDN – https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat
Came here expecting to see a way to do this trick with CSS.
I mean, if you had a tag on your website, you could use JavaScript to add a localised version of the date and this CSS as a fallback:
time:empty:after {
content: attr(datetime);
}
I’ve found that passing undefined in for locale sometimes meant that the wrong locale was being used if my language wasn’t the browser’s default, so I make sure to pass navigator.languages instead (navigator.languages || navigator.language if I need IE11 support).
Thanks for this Sarah! Just bookmarked this tab so I won’t have to keep looking it up :)
It even supports Persian Calendar and Digits. Samples:
https://gist.github.com/unicornist/0e37b3691551c42c716e96c125b99df7
That IntlDateTimeFormat is indeed very useful. In addition, I recommend the library
date-fns
https://date-fns.org/It’s also based on these and contains some other useful methods for adding/subtracting days etc as well.
Gonna bookmark this, high five to me too!