Created
October 26, 2011 07:11
-
-
Save renajohn/1315660 to your computer and use it in GitHub Desktop.
Handlebar date formatter helper
This file contains 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
/** | |
* 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