Datetime formatting / customization in ejs

0
1105

For a better code management, you can add the code(below) in app.js/server.js. This will save moment in the res.locals.moment at the time of starting the app. By doing this you can access the moment variable from any ejs page. app.js/server.js:

const express = require("express");
const app = express();
const moment = require("moment");

app.use((req, res, next)=>{
    res.locals.moment = moment;
    next();
  });

something.ejs

<p><%= moment(yourDateVariable).format('Do MMMM, YYYY')  %></p>

Here Do will result with 19th.You can check it here https://momentjs.com/docs/#/displaying/format/. Hope this will help.

LEAVE A REPLY

Please enter your comment!
Please enter your name here