How do I display todays date in Node.js Jade?


Question

I am new to Node.js and Jade and I have tried to use #{Date.now()} and it's giving me numbers. How do I display the date in mm/dd/yy format?

1
39
10/20/2014 8:53:37 AM

Accepted Answer

You can use moment.js

First, add moment to your express application locals

express = require('express');
...
app = express();
app.locals.moment = require('moment');

Then you can use moment within a jade template like this:

p #{moment(Date.now()).format('MM/DD/YYYY')}

Moment takes Date.now() by default, so yo can also write:

p #{moment().format('MM/DD/YYYY')}

Sources:

74
10/28/2013 2:52:22 PM

Or you could use the moment.js library to handle dates and format them accordingly to your needs


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon