Closed erikjung closed 9 years ago
FWIW, here's a helper I made that uses moment.js
, I believe it works how you described because if context
is null
it should just return the current date (but I may be wrong about that):
/*
* Format a date using Moment.
* Usage: {{moment date format="MMMM YYYY"}}
*/
var Handlebars = require('handlebars'),
moment = require('moment');
Handlebars.registerHelper('moment', function (context, block) {
return moment(new Date(context)).utc().format(block.hash.format);
});
Ignore or borrow what you will!
@tylersticka Thanks!
I ended up doing something slightly different in order to support optional date value input. Initially, this was indented to always output the value of new Date()
with no input needed. I thought it might be nice to handle an input value if needed, and to supply a better formatting default.
module.exports = function (context, options) {
var date = options ? context : new Date();
var options = options || context;
var format = options.hash.format || 'YYYY-MM-DD ';
// http://momentjs.com/docs/#/displaying/
return moment(date).utc().format(format);
}
Any of these use cases should work now:
{{moment}}
{{moment "2012-02-02"}}
{{moment format="YYYY"}}
{{moment "2014-05-06" format="YYYY-MM"}}
Specifically to output the build date, we need some kind of helper that outputs a formatted version of
new Date()
.Possible example: