Datetime formatting / customization in ejs

你。 提交于 2019-12-04 18:47:06

问题


I show date by this in ejs

<%= new Date();%>

it give me result

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

But I need to show as

19th January, 2016

How can I do this in ejs?


回答1:


You can use moment

In your controller,

var moment = require('moment');
exports.index = function(req, res) {
    res.render('index', { moment: moment });
}

In your html,

<html>
    <h1><%= moment().format('Do MMMM, YYYY'); %></h1>
</html>



回答2:


You don't need moment js, you can simply use this

<%= new Date().getFullYear();%>




回答3:


Post is old but in case anyone runs into the issue.

You can eliminate installing moment and write one little line of code. adding .toDateString() will provide you with the above format in ejs.

however, Moment is used for more detailed date, such as Day, or month or year etc...




回答4:


You can do this:

<%= moment(user.date).format( 'MMM-DD-YYYY') %>


来源:https://stackoverflow.com/questions/35151187/datetime-formatting-customization-in-ejs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!