Skip to content

Instantly share code, notes, and snippets.

@renajohn
Created October 26, 2011 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renajohn/1315660 to your computer and use it in GitHub Desktop.
Save renajohn/1315660 to your computer and use it in GitHub Desktop.
Handlebar date formatter helper
/**
* Date formatting helper.
*
* Date helper takes a SC.DateTime and return a formatted string based on the format
* parameter. If no format is given, it uses %c as default.
*
* @param format a format string
*/
Handlebars.registerHelper('date', function(path, block) {
if (path) {
var date = block.contexts[0].getPath(path);
var attrs = block.hash;
var format = "%c";
// if a format is provided, use it.
if (attrs && attrs.format) {
format = attrs.format;
}
// make sure the date is of type SC.DateTime
if (date && SC.kindOf(date,SC.DateTime)) {
return date.toFormattedString(format);
} else if (SC.none(date)) {
console.log('date helper is expecting a SC.DateTime object, Null or undefined');
} else {
console.log('date helper is expecting a SC.DateTime object', date);
}
}
return "";
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment