Steven
Steven

Reputation: 49

Displaying the MySQL date correctly

So I'm using this code

let obj = {print: rows}
    res.render('../views/pages/data', obj)

to render my data in a ejs file using this code

<table>
    <% print.forEach(function (student) { %>

        <tr class="table-info">
        <td><%= student.firstName %></td>
        <td><%= student.lastName %></td>

        <td><%= student.building %></td>
        <td><%= student.room %></td>
        <td><%= student.checkIn %></td>
        <td><%= student.checkOut %></td>


        </tr>
        <% }) %>

but the checkIn and checkOut date is not displayed the same way it is in MySQL. In the DB it only shows the data and time, but when I display it on the ejs file it displays checkIn and checkOut like this

Mon Nov 19 2018 12:56:09 GMT-0500 (Eastern Standard Time)   Mon Nov 19 2018 12:56:47 GMT-0500 (Eastern Standard Time)

How can I get rid of the GMT-500 (Estern Standard Time)?

Upvotes: 0

Views: 485

Answers (1)

S.Mishra
S.Mishra

Reputation: 3664

Use toUTCString() to get rid of -0500 (Eastern Standard Time) but GMT will be there. To get rid of whole GMT-0500 (Eastern Standard Time) string, you need to do manipulation like:

today.toUTCString().split('GMT')[0];

The best practice would be to manipulate DateTime into String format at server side or at least embedded JavaScript before rendering it on UI.

If you want to do it at the client (HTML) side then you need to use JavaScript string manipulation functions. So in your case, it should be like this:

<table>
    <% print.forEach(function (student) { %>

        <tr class="table-info">
        <td><%= student.firstName %></td>
        <td><%= student.lastName %></td>

        <td><%= student.building %></td>
        <td><%= student.room %></td>
        <td><%= student.checkIn.split('GMT')[0] %></td>
        <td><%= student.checkOut.split('GMT')[0] %></td>


        </tr>
        <% }) %>

I am not sure if this is the best way to accomplish what you want but if you don't want to change your code then above could be the nearest solution for your problem.

Upvotes: 1

Related Questions