Skip to content

Instantly share code, notes, and snippets.

@tcrammond
Last active October 23, 2015 15:10
Show Gist options
  • Save tcrammond/711b8965a6fc767c0f4b to your computer and use it in GitHub Desktop.
Save tcrammond/711b8965a6fc767c0f4b to your computer and use it in GitHub Desktop.
Handlebars moment date format helper
// Amended from https://gist.github.com/elidupuis/1468937
/**
* Formats the given date using moment
* Both format and default parameters are optional
* usage: {{date-format myDate format="DD MM YYYY" default="N/A"}}
*/
Handlebars.registerHelper('date-format', function(context, block) {
if (window.moment && context !== null && context !== '') {
var format = block.hash.format || "DD-MM-YYYY";
return moment(context).format(format);
}
// Moment not present or the date is empty-ish, show the default value or nothing
return (block.hash.default ? block.hash.default : context);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment