Created
June 4, 2013 20:03
-
-
Save jonsamwell/5709083 to your computer and use it in GitHub Desktop.
A couple of simple Handlebar helpers to format dates using Moment.js
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
(function (Handlebars, moment) { | |
"use strict"; | |
/** | |
* Format an ISO date using Moment.js | |
* http://momentjs.com/ | |
* moment syntax example: moment(Date("2013-03-30T19:45:23")).format("MMMM YYYY") | |
* usage: {{dateFormat somedate format="MMMM YYYY"}} | |
*/ | |
Handlebars.registerHelper('dateFormat', function (context, block) { | |
var date = context; | |
if (moment) { | |
date = moment(new Date(context)).format(block.hash.format || "MMM Do, YYYY"); | |
} | |
return date; | |
}); | |
/** | |
* Format an ISO date into calendar time using Moment.js | |
* http://momentjs.com/ | |
* moment syntax example: moment(Date("2013-03-30T19:45:23")).calendar(); | |
* usage: {{calendarTime somedate}} | |
*/ | |
Handlebars.registerHelper('calendarTime', function (context) { | |
var date = context; | |
if (moment) { | |
date = moment(new Date(context)).calendar(); | |
} | |
return date; | |
}); | |
}(Handlebars, moment)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment