Skip to main content
View on YouTube to comment and like!

Syntax Video

JS Intl API Examples - Dependency Free Formatting

Chapter 1 0:00

Intro

what's up everyone CJ here I wanted to show you something I'm working on and specifically how I'm able to format dates and numbers in this app that I'm building so I'm working on some web components that allow you to embed Blue Sky posts and threads I'll do a a deeper dive video on the blue sky API and and how I built this thing out with web components and stuff like that but what I wanted to show you today is something I'm pretty excited about which is how I'm able to format the dates and then also how I'm able to format larger numbers and automatically abbreviating them if they're over a certain amount and I'm doing this using the Intel API now the Intel API has been around since 2017 this is nothing new but I just found it really refreshing that I was able to implement all of these things in my app without installing an extra library because in the past typically you'd have to use something like momentjs or maybe these days you might use something like date functions but I was able to implement this with no extra libraries at all so let me show you how I'm doing the date formatting so here you can see I have a utils file that has multiple different formatters that I'm

Chapter 2 0:55

Intl.DateTimeFormat

using across my application I'm passing in Navigator do language and so this will set up the formatters to use the users's preferred language whatever they have set in their web browser and then you can pass in some options so for this date formatter here I'm passing in all of the options that's going to take a date and format it like so but what's great is if I change my local to something else so for instance let's swap over to Spanish now you can see that it formats it for Spanish speaking users and so this is great because literally all I had to do was pass the data in and now whatever local the user has set that visits my website it is going to see the date in their preferred format and so you can see here whenever I'm rendering it out I'm passing in an instance of a date and so the API actually gives us back just a string but that is in the correct format to be able to pass into the date Constructor so all I have to do is pass in that date to our formatter do format function and it automatically uses these options and formats it using the user's preferred local and so this is really awesome especially for other languages that might put things in a different order or have different preferences about how you should display dates so for instance let's swap over to Japanese and then we'll refresh and you can see that in

Japanese they actually put the year at the front and this is the preferred display format using those those options so yeah the date formatter is great there's a bunch of other options you can pass in here as well check out the mdn page to see all of the different options you can pass in and maybe take a look at some of the apps that you're building to see where you can potentially pull out a date formatting library and just use the Intel API instead next I want to show you how I'm using the number formatter so you can see here this specific post has a little over 3.6 th000 likes but it's actually being abbreviated with that capital K there and the thing that allows us to do this is intel. number format you can see here that I have notation set to compact but they support quite a few other notations if we set to standard you'll see that it actually shows the full number but in this case with commas because I'm in the English local but let's for instance swap back over to Spanish and then you can see in the local of Spanish they actually don't add any commas or periods for numbers that are less than uh 10,000 but if we look at a post that has lots of likes like this one you can see it has 88,000 likes and so in the Spanish local we actually see a period there but we're not seeing commas or periods for the smaller numbers and let's take a look at

Chapter 3 2:16

Intl.NumberFormat

what this looks like in German as well so you can see in a German local they actually do prefer putting periods for numbers that are greater than a thousand and so this is awesome anywhere I want to display the number I'm passing it into that number formatter and of course the common pattern you see on lots of social networking websites is to abbreviate larger numers so if we switch back to compact you can see in the German local it actually doesn't abbreviate it when we're in compact mode and that's the preferred settings for that local but if we switch to let's say like Japanese you can see in Japanese for very large numbers they add that abbreviation there so this one's great super simple to add not many options and it just works Works instantly with any number you pass into it now the last formatter I'm using is the relative time formatter you can see for all of the post replies we're actually showing how long ago that reply occurred and so this post is less than a day old so most of the repes are just a few hours old uh this particular post is a few days old you can see it says like one day ago two days ago this post is a few days old so you can see here it's saying like 4 days ago 2 days ago Etc and then here are some posts that are much older and it's saying like 1 week ago or 3 weeks ago so to do this we're using intel. relative time format and in this case I have the

Chapter 4 3:31

Intl.RelativeTimeFormat

style set to long but they also support other formats so here we could do the narrow format and that's actually going to abbreviate the time so 18h ago instead of 18 hours ago and so 4 d ago instead of 4 days ago and again this one is internationalized as well so you've probably seen it as I've been demonstrating these other languages but let's swap out to German and see what this looks like so you can see here it's been a long time since I've spoken German but as you can see it translates as well now this is the one the Intel API that actually does require a little bit of extra code and first up shout out to webdev simplified he actually has a blog post where he talks about how to do this but essentially the formatter is very simple it just takes in a unit which can be things like years seconds days hours months and then you pass in that specific unit so if you want it to say four days ago or five days ago you would actually pass in five for the number and then the unit would be days so it's not automatic you can see that we have a little bit of calculation that we need to do essentially we need to figure out what

is the total duration that we're trying to display so we get the current time and subtract that from the date that we're trying to display divide by 1 th000 to turn it into seconds and then we're going one by one to find the unit that it's less than so we see is it less than 60 seconds is it less than 60 minutes is it less than 24 hours is it less than 7 days and then when we find that particular unit that's the one that we use to format so not super straightforward but still a whole lot less code than having to bring in an entire library and we get some nice relative times there so that's all I wanted to show in this video but I am just super excited about the fact that we can do this directly in the browser now so like I said at the beginning of the video these have been around for a long time they're nothing new but I encourage you to look at some of your projects and maybe take a look can you pull out momentjs or date functions or one of these other libraries that uh is doing things that you could actually just do directly in the web browser now you can check out can I use to see support now both date time format and number format have support going all the way back to ie11 so these have been around for a very long time relative time format is a little bit newer so it doesn't support i11 these days that's not really even an issue you can see

Chapter 5 5:23

where are these supported?

that it's green across all of the modern web browsers so that's it for this video let me let me know down in the comments if you're already using the Intel API let me know down in the comments if you're going to start and uh keep on coding I'll see you in the next one [Music]

Chapter 6 5:58

thanks!

Quote it with credit and a link. Quoting Syntax