Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>JS time ago example</h1>

<h2>&lt;time&gt; datetime attribute</h2>

<table>
  <tr>
    <th>Timestamp</td>
    <th>Result</td>
  </tr>
  <tr>
    <td>2010-08-14</td>
    <td><time datetime="2010-08-14">Should be replaced</time></td>
  </tr>
  <tr>
    <td>2016-08-14</td>
    <td><time datetime="2016-08-14">Should be replaced</time></td>
  </tr>
  <tr>
    <td>2016-08-14T14:54:39-0000</td>
    <td><time datetime="2016-08-14T14:54:39-0000">Should be replaced</time></td>
  </tr>
  <tr>
    <td>2020-08-14</td>
    <td><time datetime="2020-08-14">Should be replaced</time></td>
  </tr>
  <tr>
    <td>invalid</td>
    <td><time datetime="invalid">This should not be changed as the date is invalid</time></td>
  </tr>
</table>


<h2>&lt;time&gt; value</h2>

<table>
  <tr>
    <th>Timestamp</td>
    <th>Result</td>
  </tr>
  <tr>
    <td>2010-08-14</td>
    <td><time>2010-08-14</time></td>
  </tr>
  <tr>
    <td>2016-08-14</td>
    <td><time>2016-08-14</time></td>
  </tr>
  <tr>
    <td>2016-08-14T14:54:39-0000</td>
    <td><time>2016-08-14T14:54:39-0000</time></td>
  </tr>
  <tr>
    <td>2020-08-14</td>
    <td><time>2020-08-14</time></td>
  </tr>
  <tr>
    <td>invalid</td>
    <td><time>This should not be changed as the date is invalid</time></td>
  </tr>
</table>


<h2>Norwegian translation*</h2>

<p>*My Norwegian is not great so the translations may not be very accurate.</p>

<table>
  <tr>
    <th>Timestamp</td>
    <th>Result</td>
  </tr>
  <tr>
    <td>2010-08-14</td>
    <td><time class="norsk">2010-08-14</time></td>
  </tr>
  <tr>
    <td>2016-08-14</td>
    <td><time class="norsk">2016-08-14</time></td>
  </tr>
  <tr>
    <td>2016-08-14T14:54:39-0000</td>
    <td><time class="norsk">2016-08-14T14:54:39-0000</time></td>
  </tr>
  <tr>
    <td>2020-08-14</td>
    <td><time class="norsk">2020-08-14</time></td>
  </tr>
  <tr>
    <td>invalid</td>
    <td><time class="norsk">This should not be changed as the date is invalid</time></td>
  </tr>
</table>
              
            
!

CSS

              
                h2 {
  margin: 1.75em 0 .5em
}

th {
  text-align: left;
  border-bottom: 1px solid #ccc;
  padding: .25em .25em;
}

td {
  padding: .25em .25em;
  vertical-align: top;
}

td:nth-child(1) {
  white-space: nowrap;
}

html {
  background: #f3f3f3;
}

body {
  max-width: 650px;
  padding: .5em 1.5em;
  margin: 0 auto;
  background: #fff;
  border: 1px solid #ddd;
}
              
            
!

JS

              
                /**
 * Take an RFC 3339 or ISO 8601 date and returns
 * the date in human readable form.
 *
 * Will return undefined if lacks browser support
 * or it cannot parse the date.
 *
 * @param  {string} time
 * @param  {object} [lang] Optional language object
 * @return {string|undefined}
 * @license MIT
 * @author Sam Clarke <sam@samclarke.com>
 */
function timeToWords(time, lang) {
  lang = lang || {
    postfixes: {
      '<': ' ago',
      '>': ' from now'
    },
    1000: {
      singular: 'a few moments',
      plural: 'a few moments'
    },
    60000: {
      singular: 'about a minute',
      plural: '# minutes'
    },
    3600000: {
      singular: 'about an hour',
      plural: '# hours'
    },
    86400000: {
      singular: 'a day',
      plural: '# days'
    },
    31540000000: {
      singular: 'a year',
      plural: '# years'
    }
  };

  var timespans = [1000, 60000, 3600000, 86400000, 31540000000];
  var parsedTime = Date.parse(time.replace(/\-00:?00$/, ''));

  if (parsedTime && Date.now) {
    var timeAgo = parsedTime - Date.now();
    var diff = Math.abs(timeAgo);
    var postfix = lang.postfixes[(timeAgo < 0) ? '<' : '>'];
    var timespan = timespans[0];

    for (var i = 1; i < timespans.length; i++) {
      if (diff > timespans[i]) {
        timespan = timespans[i];
      }
    }

    var n = Math.round(diff / timespan);

    return lang[timespan][n > 1 ? 'plural' : 'singular']
      .replace('#', n) + postfix;
  }
}

var norwegian = {
  postfixes: {
    '<': ' siden',
    '>': ' fra nå'
  },
  1000: {
    singular: 'et øyeblikk',
    plural: 'et øyeblikk'
  },
  60000: {
    singular: 'omtrent et minutt',
    plural: '# minutter'
  },
  3600000: {
    singular: 'omtrent en time',
    plural: '# timer'
  },
  86400000: {
    singular: 'en dag',
    plural: '# dager'
  },
  31540000000: {
    singular: 'et år',
    plural: '# år'
  }
};

document.addEventListener('DOMContentLoaded', function() {
  var elements = document.getElementsByTagName('time');
  for (var i = 0; i < elements.length; i++) {
    var time = elements[i];
    // The date should be either in the datetime attribute
    // or in the text contents if no datetime attribute
    var date = time.getAttribute('datetime') || time.textContent;
    var lang = time.className === 'norsk' ? norwegian : null;

    var dateInWords = timeToWords(date, lang);
    if (dateInWords) {
      time.textContent = dateInWords;
    }
  }
});
              
            
!
999px

Console