UNPKG

helper-dateformat

Version:

Template helper for adding formatted dates using node-dateformat. Works with Handlebars, Lo-Dash, underscore, or any template engine that supports helper functions. Also compatible with verb, assemble and Template.

137 lines (89 loc) 5.1 kB
# helper-dateformat [![NPM version](https://img.shields.io/npm/v/helper-dateformat.svg?style=flat)](https://www.npmjs.com/package/helper-dateformat) [![NPM downloads](https://img.shields.io/npm/dm/helper-dateformat.svg?style=flat)](https://npmjs.org/package/helper-dateformat) [![Linux Build Status](https://img.shields.io/travis/helpers/helper-dateformat.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/helper-dateformat) > Template helper for adding formatted dates using node-dateformat. Works with Handlebars, Lo-Dash, underscore, or any template engine that supports helper functions. Also compatible with verb, assemble and Template. ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm install --save helper-dateformat ``` See [node-dateformat](https://github.com/felixge/node-dateformat) for all available options. ## Usage This helper should work with any template engine, or as a javascript utility. ```js var dateformat = require('helper-dateformat'); console.log(dateformat('today')); //=> December 24, 2016 ``` ### [handlebars](http://www.handlebarsjs.com/) usage ```js var handlebars = require('handlebars'); handlebars.registerHelper('dateformat', require('helper-dateformat')); ``` Usage ```html {{dateformat "MMMM DD, YYYY"}} ``` ### [lodash](https://lodash.com/) and [underscore](http://underscorejs.org) usage ```js var helper = require('helper-dateformat'); // as a mixin _.mixin({dateformat: helper}); _.template('<%= dateformat("MMMM DD, YYYY") %>', {}); // passed on the context _.template('<%= dateformat("MMMM DD, YYYY") %>', {dateformat: helper}); // as an import var settings = {imports: {dateformat: helper}}; _.template('<%= dateformat("MMMM DD, YYYY") %>', {}, settings); ``` ### [templates](https://github.com/jonschlinkert/templates) usage Register the helper for use with any template engine ```js template.helper('dateformat', require('helper-dateformat')); ``` ### [assemble](https://github.com/assemble/assemble) usage To register the helper for use with [assemble](https://github.com/assemble/assemble) v0.6.x: ```js assemble.helper('dateformat', require('helper-dateformat')); ``` ### [verb](https://github.com/verbose/verb) usage In `verbfile.js`: ```js module.exports = function(verb) { verb.helper('dateformat', require('helper-dateformat')); verb.task('default', function() { return verb.src('*.md') .pipe(verb.renderFile()) .pipe(verb.dest('.')); }); }; ``` Templates: ```js {%= dateformat("MMMM DD, YYYY") %} ``` ## About ### Related projects * [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/assemble/handlebars-helpers) | [homepage](https://github.com/assemble/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.") * [helper-date](https://www.npmjs.com/package/helper-date): Format dates with date.js and moment.js. Uses date.js to parse human readable date phrases, and… [more](https://github.com/helpers/helper-date) | [homepage](https://github.com/helpers/helper-date "Format dates with date.js and moment.js. Uses date.js to parse human readable date phrases, and moment to format the rendered output. Should work with any Handlebars, Lo-Dash, underscore, or any template engine that allows helper functions to be registere") * [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.") ### Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). ### Building docs _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ To generate the readme and API documentation with [verb](https://github.com/verbose/verb): ```sh $ npm install -g verb verb-generate-readme && verb ``` ### Running tests Install dev dependencies: ```sh $ npm install -d && npm test ``` ### Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) ### License Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). Released under the [MIT license](LICENSE). *** _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.2.0, on December 25, 2016._