Datetime formatting / customization in ejs

后端 未结 6 1912
清歌不尽
清歌不尽 2021-02-07 04:09

I show date by this in ejs

<%= new Date();%>

it give me result

Tue Feb 02 2016 16:02:24 GMT+0530 (IST)

6条回答
  •  野的像风
    2021-02-07 05:12

    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

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

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

提交回复
热议问题