User:Enterprisey/schedule-local-time.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.hook("wikipage.content").add(function(){
function fixScheduleTime(time){
  if(time.indexOf("-")>=0) {
    var times = time.split("-");
    return fixScheduleTime(times[0]) + " – " + fixScheduleTime(times[1]) + " (" + times[0] + " – " + times[1].trim() + ")";
  } else {
    var parts = time.split(":");
    var date = new Date();
    if(parts.length > 1) {
      date.setUTCHours((parseInt(parts[0]) + 5) % 24);
      date.setMinutes(parseInt(parts[1].trim()));
    }
    return date.toLocaleTimeString().replace( /(\d?\d:\d\d):\d\d/, "$1" );
  }
}

$("table.wikitable").each(function(_idx,table){
  Array.prototype.slice.call(table.rows).forEach(function(row,idx){
    if(idx>0)row.cells[0].textContent = fixScheduleTime(row.cells[0].textContent)
  })
});
});