Skip to content

Instantly share code, notes, and snippets.

Created August 28, 2014 17:24
Show Gist options
  • Select an option

  • Save anonymous/dca9be0cde7a03fc6f7b to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/dca9be0cde7a03fc6f7b to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Aug 28, 2014.
    86 changes: 86 additions & 0 deletions time.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    <script type="text/javascript">
    (function() {
    "use strict";
    /* From https://github.com/dperini/ContentLoaded/blob/master/src/contentloaded.js
    * Author: Diego Perini (diego.perini at gmail.com)
    * License: MIT
    */
    function contentLoaded(win, fn) {

    var done = false, top = true,

    doc = win.document, root = doc.documentElement,

    add = doc.addEventListener ? 'addEventListener' : 'attachEvent',
    rem = doc.addEventListener ? 'removeEventListener' : 'detachEvent',
    pre = doc.addEventListener ? '' : 'on',

    init = function(e) {
    if (e.type == 'readystatechange' && doc.readyState != 'complete') return;
    (e.type == 'load' ? win : doc)[rem](pre + e.type, init, false);
    if (!done && (done = true)) fn.call(win, e.type || e);
    },

    poll = function() {
    try { root.doScroll('left'); } catch(e) { setTimeout(poll, 50); return; }
    init('poll');
    };

    if (doc.readyState == 'complete') fn.call(win, 'lazy');
    else {
    if (doc.createEventObject && root.doScroll) {
    try { top = !win.frameElement; } catch(e) { }
    if (top) poll();
    }
    doc[add](pre + 'DOMContentLoaded', init, false);
    doc[add](pre + 'readystatechange', init, false);
    win[add](pre + 'load', init, false);
    }

    }

    function utcnow() {
    var now = new Date();
    var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
    return now_utc;
    }

    // The following function is under MIT License - http://www.opensource.org/licenses/mit-license.php
    // Adapted from https://github.com/rmm5t/jquery-timeago/blob/master/jquery.timeago.js
    function prettyTimeDiff(then, now) {
    var diff = now - then;
    var seconds = Math.abs(diff) / 1000;
    var minutes = seconds / 60;
    var hours = minutes / 60;
    var days = hours / 24;
    var years = days / 365;
    var words;

    true && seconds < 60 && (words = seconds + 'seconds') ||
    seconds < 120 && (words = '1 minute') ||
    minutes < 60 && (words = Math.round(minutes) + ' minutes') ||
    hours < 24 && (words = Math.round(hours) + ' hours') ||
    days < 2 && (words = '1 day') ||
    days < 31 && (words = Math.round(days) + ' days') ||
    days < 365 && (words = Math.round(days / 30) + ' months') ||
    years < 2 && (words = '1 year') ||
    (words = Math.round(years) + ' years');

    return words + ' ago';
    }

    function relTime() {
    var now = utcnow();
    var elements = document.querySelectorAll('span.posttime');
    var d, item, dstring;
    for(var i = 0; i < elements.length; i++) {
    item = elements[i];
    dstring = item.getAttribute('data-ms');
    d = new Date(dstring);
    item.innerHTML = prettyTimeDiff(now, d);
    item.setAttribute('title', dstring);
    }
    }
    contentLoaded(window, relTime);
    }());
    </script>