Skip to content

Instantly share code, notes, and snippets.

@jonsamwell
Created June 4, 2013 20:03
Show Gist options
  • Save jonsamwell/5709083 to your computer and use it in GitHub Desktop.
Save jonsamwell/5709083 to your computer and use it in GitHub Desktop.
A couple of simple Handlebar helpers to format dates using Moment.js
(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