0

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)?

Steven
  • 49
  • 1
  • 9
  • have you consider sequelize ? https://sequelize.readthedocs.io/en/v3/ – Amit Bhoyar Nov 19 '18 at 20:36
  • Thank you, I found a way looking into squelize, but know I have a new problem. I eddited my post. – Steven Nov 19 '18 at 21:20
  • Possible duplicate of [Datetime formatting / customization in ejs](https://stackoverflow.com/questions/35151187/datetime-formatting-customization-in-ejs) – mlt Nov 19 '18 at 22:51

1 Answers1

1

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.

S.Mishra
  • 3,394
  • 28
  • 20
  • if I use the .split('GMT')[0] I get 'student.checkIn.split is not a function' – Steven Nov 19 '18 at 22:23
  • When I console.log the the result using rows[0].checkIn for example it only displays the date and time there is no GMT and excess – Steven Nov 19 '18 at 22:32
  • <%= data.checkOut.toUTCString().split('GMT') %> this kind of works. It leaves me with a , after the time – Steven Nov 19 '18 at 22:36
  • Then Split with `, GMT` and it should work. That means: `<%= data.checkOut.toUTCString().split(', GMT') %>` – S.Mishra Nov 19 '18 at 22:40
  • I decided to get rid of the , and keep the GMT and just reduce the font size – Steven Nov 19 '18 at 23:14