Feb 17, 2016

Javascript world clock with automatic time zones

One would imagine that getting a world clock - real time with automatic time zones including daylight savings - wouldn't be that difficult to include on your web site. On a standalone web site it probably isn't since there are several free or low cost solutions available. But in SharePoint it wasn't quite as straightforward.

One of my customers, a global company, already has one solution implemented on their intranet site, but there are two problems with it: it only refreshes the time on page refresh and the daylight savings (i.e. offset for each time zone) need to be adjusted manually every half a year. So I began to look into the options for modernizing this world clock of theirs.

First thing I realized was that most of the ready made solutions rely on PHP. This is already a no-no in SharePoint. Then I came accross this very cool site http://www.clocklink.com/ which has built in time zones and a multitude of analaog clocks (using html5 or flash). These could be used in SharePoint quite easily per se, and I even found a pretty neat solution using a SharePoint list to create a multitude of easily maintainable clocks. Check it out at Path to SharePoint

However, this requires cross-domain queries and moreover, when working in e.g. SharePoint Online, queries from https to http wich pretty much is a showstopper here. 

At this point I was starting to be pretty frustrated. Being so close and still so far away from a working solution. Then along came moment.js. A life saver, so to speak. Plus a very elegantly written tutorial for creating an analog clock using javascript and html5 canvas.

Combining all this plus a little bit of jquery I finally came up with a fully SharePoint (Online) compatible world clock solution (though I actually ripped the canvas part away, as I only needed a digital time display with some additional info). So, let's do a walk through for the benefit of the next one needing to do something similar:

1) Download moment.js (or min) and the suitable version of moment-timezone (from the Moment and Moment Timezone pages). Optionally also download jquery-file to avoid any cross-domain calls.

2) Upload the js-files into a suitable library in SharePoint, eg. Site Assets on the site.

3) Create a test file for compiling all the needed code to add to the script editor web part on your page and first off, add references to the js-files in the SharePoint library.

<script type="text/javascript" src="../SiteAssets/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="../SiteAssets/moment.min.js"></script>
<script type="text/javascript" src="../SiteAssets/moment-timezone-with-data.min.js"></script>

4) Add a bit of CSS:

<style type="text/css">
.current-time {
display: block;
font-weight: bold;
text-align: center;
width: 200px;
padding: 10px;
}
.clockcontainer {
  background-color: lightblue;
  border: 1px solid blue;
  border-radius: 5px;
  float: left;
  margin: 5px;
}
</style>

5) Add the javascript logic:

<script type="text/javascript">
document.addEventListener('DOMContentLoaded', startTimer);
function startTimer() {
    setInterval(displayTime, 1000);
    displayTime();

function displayTime() {
jQuery(".clockcontainer").each(function(){
var timezone = jQuery(this).attr("title");
var zonetitle = jQuery(this.firstElementChild).attr("title");
var now = moment().tz(timezone).format("h:mm:ss A");
var day = moment().tz(timezone).format("dddd MMMM DD, YYYY");

jQuery(this.firstElementChild).html(zonetitle + "<br/>" + day + "<br/>" + now);
});
}
</script>

(Note the jQuery instead of $ - in SharePoint this generally works more reliably)

6) Copy and paste all of the above to a Script Editor web part on the page where you are adding the world clocks, preferably somewhere close to the bottom of the page.

7. Add a Content Editor web part on the page where you want to display the clocks.

8) Edit the source of the CEWP content and add the clock elements (as many as you wish, but note the highlighted parts):

<div class="clockcontainer" title="Europe/London">
<div class="current-time" title="London">
time</div>
</div>

9) Modify each element to put out the correct time zone (title of the clockcontainer element) and the label for the clock (title of the current-time element). Check proper time zone names at the momentjs site.

That should be it, folks. Now, your SharePoint page should display correct times for your chosen time zones, e.g.


Momentjs offers plenty of options for time and date formatting. 

7 comments:

Anonymous said...

Thanks Saana, this is just what I need! However when I implement it on my SharePoint page I get a blue box as in your working example, but the contents shows 'time' instead of 'London Monday December 05,...' Do you know why this would be? Any help would be great!!

Eve

SM said...

Hi Eve! Seems that some detail has been neglected - either a typo somewhere or something is missing on your page. Double check for typos and check that all files are in place and timezone titles are correct. Your javascript is either not finding the element on the page or more probably not reading the moment.js or jquery files. Hope you find the error!

Anonymous said...

Hi Saana. It is now displaying correctly - silly mistake!

Thanks for your help.
E

SM said...

Excellent :)

Anonymous said...

Hi Eve,
I have the same issue in that it just shows 'time' but I cannot see where the problem is. Can you tell me how you solved it?
thanks,
Jeanette

Anonymous said...

i am trying to use the exact code you have here. but I am missing something, can you help?

Anonymous said...

I am trying to add this to me SharePoint Masterpage and I keep getting SCRIPT1002: Syntax error when I try to call the script tags. any advice?