Monchoman45

JS_clock

May 26th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var JS_clock = {
  2.     clocks: [],
  3.     interval: 0,
  4. };
  5.  
  6. JS_clock.load = function() {
  7.     if(window.skin == 'oasis') {var body = 'WikiaArticle';}
  8.     else {var body = 'bodyContent';}
  9.  
  10.     var spans = document.getElementById(body).getElementsByTagName('span');
  11.     for(var i = 0; i < spans.length; i++) {
  12.         if(spans[i].classList.contains('js-clock')) {JS_clock.clocks.push(spans[i]);}
  13.     }
  14.     JS_clock.update();
  15.     setInterval(JS_clock.update, 1000);
  16. }
  17.  
  18. JS_clock.update = function() {
  19.     for(var i = 0; i < JS_clock.clocks.length; i++) {
  20.         var timezone = JS_clock.clocks[i].getAttribute('data-timezone') * 1;
  21.         if(!timezone) {timezone = 0;}
  22.         var date = new Date();
  23.         date.setUTCHours(date.getUTCHours() + timezone);
  24.  
  25.         var hours = date.getUTCHours();
  26.         if(hours < 10) {hours = '0' + hours;}
  27.         var minutes = date.getUTCMinutes();
  28.         if(minutes < 10) {minutes = '0' + minutes;}
  29.         var seconds = date.getUTCSeconds();
  30.         if(seconds < 10) {seconds = '0' + seconds;}
  31.         var month = wgMonthNamesShort[date.getUTCMonth() + 1];
  32.         var day = date.getUTCDay();
  33.         var tz = '(UTC ';
  34.         if(timezone < 0) {tz += timezone + ')';}
  35.         else {tz += '+' + timezone + ')';}
  36.  
  37.         JS_clock.clocks[i].textContent = '' + hours + ':' + minutes + ':' + seconds + ', ' + month + ' ' + day + ' ' + tz;
  38.     }
  39. }
  40.  
  41. if(document.readyState == 'complete') {JS_clock.load();}
  42. else {window.addEventListener('load', JS_clock.load);}
Add Comment
Please, Sign In to add comment