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?
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?
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>
Using basic JS
const suffixMap = {
one: 'st',
two: 'nd',
few: 'rd',
other: 'th',
};
const date = new Date();
const dateDay = date.getDate();
const dateMonth = date.toLocaleString('default', {month: 'long'});
const dateYear = date.getFullYear();
const pluralRule = new Intl.PluralRules('en-GB', {type: 'ordinal'});
const dateOrdinal = suffixMap[pluralRule.select(dateDay)]
const ordinalDateString = `${dateDay}${dateOrdinal} ${dateMonth}, ${dateYear}`;
// Expected output: 25th August, 2020
(Adapted from this answer)
const date = new Date();
const dateDay = date.getDate();
const dateMonth = date.toLocaleString('default', {month: 'long'});
const dateYear = date.getFullYear();
// DETERMINE DATE ORDINAL
let dateOrdinal = 'th';
dateOrdinal = ([1, 21, 31].indexOf(dateDay) > -1) ? 'st' : dateOrdinal;
dateOrdinal = ([2, 22].indexOf(dateDay) > -1) ? 'nd' : dateOrdinal;
dateOrdinal = ([3, 23].indexOf(dateDay) > -1) ? 'rd' : dateOrdinal;
// FORMAT DATE AS STRING
const ordinalDateString = `${dateDay}${dateOrdinal} ${dateMonth}, ${dateYear}`;
// Expected output: 25th August, 2020
Or, 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
You don't need moment js, you can simply use this
<%= new Date().getFullYear();%>
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...
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.
You can do this:
<%= moment(user.date).format( 'MMM-DD-YYYY') %>
The problem with most of the answers on this page is that they will format the date using the server's locale, but you probably want them rendered in the user's locale. For example, if your server is in Virginia, your dates will be displayed in English in Eastern Time, but a user in another country will probably want them displayed differently.
The following code snippet will render date
using the user's locale. There's room for improvement, but it's a starting point:
<p>
<script type="text/javascript">
document.write(
new Date("<%= date.toISOString() %>").toLocaleDateString()
);
</script>
</p>
This takes the date on the server, converts it to an ISO-8601 string, and interpolates it into a little JavaScript snippet that runs client-side to produce the correct localized output.
For more information on toLocaleString, see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleString
document.write
and use .innerText
instead –
Marathi // I have tested this in a for loop, it seems to be ok.
<% let d = new Date(); %>
<% let day = d.getDate(); %>
<% let m = ["January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"]; %>
<% let month = m[d.getMonth()]; %>
<% let year = d.getFullYear(); %>
<% let newdate = day + "th " + month + ", " + year; %>
<%= newdate %> // 22th July, 2022
// You can use the if...else statement for the ordinal number
<h1>
<%= new Date().getDate() %> <%= new Date().toLocaleString('default', {month: 'long'}); %>,<%= new Date().getFullYear(); %>
</h1>
My use case required populating the post's createdAt time to a HTML file in a human readable format. Here is a sample code snippet without using the moment.js
<h6 class="text-xs text-zinc-600"><%= post.createdAt.toDateString() %></h6>
© 2022 - 2024 — McMap. All rights reserved.