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

前端 未结 7 1360
囚心锁ツ
囚心锁ツ 2021-01-31 15:04

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?

7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-31 16:00

    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:

    • Making use of utility libraries in server-side Jade templates
    • Moment.js

提交回复
热议问题