Datetime formatting / customization in ejs

ejs
user5825492 picture user5825492 · Feb 2, 2016 · Viewed 22.8k times · Source

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?

Answer

gmuraleekrishna picture gmuraleekrishna · Feb 2, 2016

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>

EDIT :

If you can live without ordinal day (th, nd, rd etc) you could use basic JS

<%= new Intl.DateTimeFormat('en-GB', { year: 'numeric', month: 'long', day: '2-digit'}).format(new Date()) %>
// Expected output: 25 August 2020