{"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 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 Package or link those up however you will.<\/p>\n Might as well stash it in localStorage after you’ve gotten it. That requires less effort on the browser’s part to retrieve next time. <\/p>\n Moment expects a date\/time value (in UTC<\/a>) like:<\/p>\n If we’re only interested in the time, we can create the date portion of that string from Moment itself of today’s date (just makes the formatting way easier). The date actually kinda matters since UGHGKGJHGH daylight savings.<\/p>\n Then we’ll create the final string, assuming we have a variable with the time we want:<\/p>\n And turn it into a Moment object:<\/p>\n Now we can adjust\/localize that time with Moment Timezone:<\/p>\n And properly format it for display:<\/p>\n Now we can drop it on the page anywhere we want. If you output the time zone as well, you could even replace the time currently output on the page, since even if detecting the time zone fails it will fall back to UTC and output that as the time zone.<\/p>\n Sometimes it makes sense (perhaps from a CMS perspective) to ask people to input time in a local time zone. But in order to do the conversion correctly, it makes more sense to have that time in UTC. <\/p>\n Moment itself can help with adjusting times, like:<\/p>\n Server side languages can help too. For instance PHP can do math on times:<\/p>\n It probably makes the most sense to do that with Moment Timezone<\/a>, because it’s not always an exact formula (again UGHADKGHK daylight savings). <\/p>\n In this demo we use an HTML5 time input and adjust it on the fly to a localized time.<\/p>\n See the Pen Adjusting Times to Local Time Zones<\/a> by Chris Coyier (@chriscoyier<\/a>) on CodePen<\/a>.<\/p>\n Do you have any experience with this? How did you handle it? <\/p>\nLibraries<\/h3>\n
\n
Step 1: Get the Timezone<\/h3>\n
if (!sessionStorage.getItem('timezone')) {\r\n var tz = jstz.determine() || 'UTC';\r\n sessionStorage.setItem('timezone', tz.name());\r\n}\r\nvar currTz = sessionStorage.getItem('timezone');<\/code><\/pre>\n
Step 2: Get a Time Value<\/h3>\n
2015-08-12T14:30Z<\/code><\/pre>\n
var date = moment().format(\"YYYY-MM-DD\");<\/code><\/pre>\n
var stamp = date + \"T\" + theTime + \"Z\";<\/code><\/pre>\n
var momentTime = moment(stamp);<\/code><\/pre>\n
Step 3: Localize the time<\/h3>\n
var tzTime = momentTime.tz(currTz);<\/code><\/pre>\n
var formattedTime = tzTime.format('h:mm A');<\/code><\/pre>\n
Step 4: Use it<\/h3>\n
output.textContent = \"Time in \" + currTz + \": \" + formattedTime;<\/code><\/pre>\n
Adjusting Times<\/h3>\n
moment().subtract(4, 'hours');\r\nmoment().add(3, 'hours');<\/code><\/pre>\n
<?php var $sixHoursAgo = strtotime(\"-6 hours\", time()); ?><\/code><\/pre>\n
Demo<\/h3>\n
Other Ways<\/h3>\n