Last active
October 23, 2015 15:10
-
-
Save tcrammond/711b8965a6fc767c0f4b to your computer and use it in GitHub Desktop.
Handlebars moment date format helper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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