3.0.3 • Published 4 years ago

browser-date-formatter v3.0.3

Weekly downloads
10,558
License
MIT
Repository
github
Last release
4 years ago

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

3.0.3

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

5 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago