Formatting Dates Using Moment.js, Handlebars, Express, and Node.js

For those of you out there that may be struggling with this, here’s a quick breakdown of how to format dates using moment.js, handlebars, express, and node.js. This post assumes you have the following packages installed in your express app:

  1. Express3 Handlebars (link)
  2. Moment.js (link)

Once you’ve included these packages in your app.js file, you’ll need to set up handlebars like so:

app = express();
hbsEngine = expressHbs.create({
    extname: 'hbs',
    defaultLayout: 'layout.hbs',
    helpers: {
        formatDate: function (date, format) {
            return moment(date).format(format);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('hbs', hbsEngine.engine);
app.set('view engine', 'hbs');

You can ignore the extname and defaultLayout configs incase you want to use the default values that handlebars sets for you for the extension and layout file name. The important thing to note is the helpers object. Basically, it’s the equivalent of the registerHelpers method in the JS version of Handlebars. In this example, I’m create a formatDate method that takes in a date and format string and returns a formatted date via moment.js.