{"id":206436,"date":"2015-08-12T10:56:35","date_gmt":"2015-08-12T17:56:35","guid":{"rendered":"http:\/\/css-tricks.com\/?p=206436"},"modified":"2015-08-12T10:56:35","modified_gmt":"2015-08-12T17:56:35","slug":"localizing-times-in-javascript","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/localizing-times-in-javascript\/","title":{"rendered":"Localizing Times in JavaScript"},"content":{"rendered":"

Say you have a time<\/strong> you’d like to display on your site. You’ve entered it in your<\/em> time zone. You can always be specific and say something like: 3:00 PM Eastern Standard Time<\/strong>. Then leave it up to whoever is reading to convert it to their local time. Every Time Zone<\/a> is a great site for that.<\/p>\n

But it can be nice to localize the time for<\/em> the reader. Time zone conversion is notoriously confusing. And it’s the type of thing computers are good at. JavaScript know what time zone the reader is in, after all.<\/p>\n

<\/p>\n

I’m sure there are a number of ways to do this. I had to do it recently on a new project, and I followed Dave Rupert’s lead, who implemented it on ShopTalk’s website<\/a> to display the localized time for our next live show.<\/p>\n

Libraries<\/h3>\n

Moment.js<\/a> and Moment Timezone<\/a> are a pair that can get this done. And if you need to do any other heavier date and time manipulation or relative calculations (e.g. “32 minutes ago”, “12 de Agosto de 2015”), you’ll be in good hands with these libraries.<\/p>\n

We’ll also need to detect the time zone itself, and thankfully there is a library for that<\/a>.<\/p>\n

So we’ll be using:<\/p>\n