Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Express.js blog application — Formatting dates

Express.js blog application — Formatting dates


I am in the process of learning Node.js/Express.js, and as an experiment I am attempting to build a small blog application.

I render the page, passing the articles:

response.render('index.jade', {
    title: 'Blog'
  , locals: {
      articles: articles

Here's my Jade template for that:

h1= title
  h1 Articles
  each article in articles
      a(href='/article/' + article.slug)= article.title
      span  at #{article.created_at}

This displays a list of articles with the title and date. However, I need a way of formatting the date into a more readable format. I have discovered libraries that allow you to do this, but my real question is how I should go about integrating this into my template? I can either pass the moment module to the template and format the date inline there, or I can format it in the route and add it onto the article object. How would you do this?

Problem courtesy of: user1082754


I have the following helpers in my application:

date: function (date) {
  return moment(date).format('YYYY/MM/DD HH:mm:ss');

fromNow: function(date) { 
  return moment(date).fromNow(); 

This could depend on your application but I didn't see the need of using different date formats all over my site.

Solution courtesy of: Pickels


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Express.js blog application — Formatting dates


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription