Skip to content

Instantly share code, notes, and snippets.

@tylersticka
Last active August 29, 2015 14:22
Show Gist options
  • Save tylersticka/00c9198caef663d62260 to your computer and use it in GitHub Desktop.
Save tylersticka/00c9198caef663d62260 to your computer and use it in GitHub Desktop.
Simple moment helper for Handlebars
/*
* Format a date using Moment.
* Usage: {{moment date format="MMMM YYYY"}}
*/
var Handlebars = require('handlebars'),
_ = require('lodash'),
moment = require('moment');
Handlebars.registerHelper('moment', function (context, block) {
var m, format;
block = block || context;
if (_.has(block, 'hash.format')) {
format = block.hash.format;
}
m = moment.utc(context);
if (! m.isValid()) {
m = moment.utc();
}
return m.format(format);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment