Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to format a date in embeddedjs

app.js

app.get('/user.html', function(req, res){
    dbConnect.collection("users").find().toArray(function(err, docsData) {
        res.render('user', {
            data: docsData,
            title: "EJS example",
            header: "Some users"
        });
    });
});

user.html

<% data.forEach(function(user){ %>
    <tr>
        <td>
            <%= user.date %>
        </td>
    </tr>
<% }) %>

output is 2014-12-24T09:47:07.436Z

this is the value coming from mongodb. I want to format this to Dec-24-2014. How to format it in embeddedjs.

like image 880
Nivin Avatar asked Dec 24 '14 10:12

Nivin


People also ask

How do you format the date in Dart flutter?

To format DateTime in Flutter using a standard format, you need to use the intl library and then use the named constructors from the DateFormat class. Simply write the named constructor and then call the format() method with providing the DateTime.


1 Answers

You can use toDateString() to better format date in JavaScript :

<% data.forEach(function(user){ %>
    <tr>
        <td>
            <%= user.date.toDateString() %>
        </td>
    </tr>
<% }) %>

If you want to display date in a custom format, you can use third party module like Moment.js. Using Moment.js your code would be like following:

app.js

var moment = require('moment');
app.get('/user.html', function(req, res){
    dbConnect.collection("users").find().toArray(function(err, docsData) {
        res.render('user', {
            data: docsData,
            title: "EJS example",
            header: "Some users",
            moment: moment
        });
    });
}); 

user.html

<% data.forEach(function(user){ %>
    <tr>
        <td>
            <%= moment(user.date).format( 'MMM-DD-YYYY') %>
       </td>
    </tr>
<% }) %> 

Hope this help!

like image 98
Zan RAKOTO Avatar answered Oct 22 '22 08:10

Zan RAKOTO