/helper-moment

Template helper for formatting dates with moment.js. Works with Handlebars, Lo-Dash or any template engine that supports helper functions.

Primary LanguageJavaScriptMIT LicenseMIT

helper-moment NPM version

Template helper for formatting dates with moment.js. Works with Handlebars, Lo-Dash or any template engine that supports helper functions.

Install with npm

npm i helper-moment --save

Run tests

npm test

Register the helper

This should work with any engine, here are a few examples

template

Register the helper for use with any template engine

template.helper('moment', require('helper-moment'));

assemble

To register the helper for use with assemble v0.6.x:

assemble.helper('moment', require('helper-moment'));

verb

Register the helper for use with verb:

var verb = require('verb');
verb.helper('moment', require('helper-moment'));

verb.task('default', function() {
  verb.src('.verb*.md')
    .pipe(verb.dest('./'));
});

handlebars

var handlebars = require('handlebars');
handlebars.registerHelper('moment', require('helper-moment'));

Lo-Dash or underscore

// as a mixin
_.mixin({moment: momentHelper});
_.template('<%= _.moment("YYYY") %>', {});
//=> '2015'

// passed on the context
_.template('<%= moment("YYYY") %>', {moment: momentHelper});
//=> '2015'

// as an import
var settings = {imports: {moment: momentHelper}};
_.template('<%= moment("YYYY") %>', {}, settings);
//=> '2015'

Example usage

With Handlebars:

{{moment "YYYY"}}
//=> 2015

With Lo-Dash or Underscore:

<%= moment("YYYY") %>
//=> 2015

With Verb (lo-dash, with special delimiters to avoid delimiter collision in markdown docs):

{%= moment('2015') %}
//=> 2015

Examples

{{moment date "YYYY"}}
{{moment date "MM"}}
{{moment date endOf="week" format="dddd, DD MMMM YYYY HH:mm:ss ZZ"}}
{{moment date format="dddd"}}-{{moment date add=daysadd format="dddd"}}
{{moment date format="dddd, DD MMMM YYYY HH:mm:ss ZZ"}}
{{moment date format="HH:mm:ss"}}
{{moment date isValid=true}}
{{moment date lang="fr" format="dddd, DD MMMM YYYY HH:mm:ss ZZ"}}
{{moment date lang="fr" format="MMMM"}}
{{moment date startOf="month" format="dddd, DD MMMM YYYY HH:mm:ss ZZ"}}
{{moment date subtract=fiveyearsago format="YYYY"}}-{{moment date format="YYYY"}}
{{moment date utc=null format="dddd, DD MMMM YYYY HH:mm:ss ZZ"}}
{{moment format="MMMM YYYY"}}
{{moment daysInMonth=true}}
{{moment diff=date}}
{{moment endOf="week" fromNow=true}}
{{moment date format="HH:mm:ss"}}
{{moment from=date}}
{{moment unixtimestamp format="HH:mm:ss"}}
{{moment}}

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue

To request or contribute a helper to the github.com/helpers org, please read this contributing guide to get started.

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert
Released under the MIT license


This file was generated by verb on December 06, 2014. To update, run npm i -g verb && verb.