browser-date-formatter

3.0.3 • Public • Published

browser-date-formatter

Format dates real fancy-like in the browser. Good for search engines and humans.

This was originally implemented on the npmjs.com website, then proved useful enough to extract into a standalone module.

Weighs in at 4K, minified and gzipped.

Installation

npm install browser-date-formatter --save

Usage

Specify date and format data attributes on all HTML elements you want formatted:

<span data-date="Thu Apr 07 2016 17:00:00 GMT-0700 (PDT)" data-format="%Y-%m-%d">
  (any textContent you put here will be replaced with "2016-04-07")
</span>

The date data attribute should be a stringified representation of a JavaScript Date object, e.g. String(new Date())

The format data attribute is optional. If omitted, a human-friendly relative date string like "5 minutes ago" is displayed. If specified, the format is expected to be a strftime pattern, such as %Y-%m-%d.

Require and invoke the function in your javascript code:

require('browser-date-formatter')()

When the DOM is ready, this module finds all elements with a data-date attribute and converts their text content to the given data-format. The formatter function is executed every 5 seconds to keep relative dates fresh.

Usage without browserify

If you're not using browserify in your project, you can use the unpkg.com hosted version instead:

<script src="https://unpkg.com/browser-date-formatter/dist.js"></script>
<script>
  browserDateFormatter()
</script> 

Tests

npm install
npm test

Dependencies

License

MIT

Generated by package-json-to-readme

Package Sidebar

Install

npm i browser-date-formatter

Weekly Downloads

1,668

Version

3.0.3

License

MIT

Unpacked Size

41.1 kB

Total Files

7

Last publish

Collaborators

  • zeke