0

I have a table where one of the columns is entered as CURRENT_TIMESTAMP.

I am pulling the data from the table and displaying it on my site. But can't figure out how to convert the date into a more reader friendly format.

Right now it looks like this: 2020-09-17T19:56:24.000Z (I also need to figure out how to convert to my timezone.)

But I want it to display like this: 09-17-2020 19:56 (would even prefer 07:56pm, but not critical)

I've seen a few examples where you can reformat it during the query. But all the examples I've seen are pulling just the date. I'm pulling everything. I'm fine with running a function on it after the query to convert... but I haven't been able to figure it out.

Adam Norton
  • 512
  • 2
  • 5
  • 21
  • 1
    This is a duplicate of [Converting a string to a date in JavaScript](https://stackoverflow.com/q/5619202/215552) and [How to format a JavaScript date](https://stackoverflow.com/q/3552461/215552). – Heretic Monkey Sep 17 '20 at 23:13
  • Please read this. https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format What you require is commonplace. – O. Jones Sep 17 '20 at 23:14

1 Answers1

0

You could use a simple function using the built-in methods available to Date objects in js. Something like this is probably what you're looking for.

function formatDate(date){
const month = date.getUTCMonth()+1;
const day = date.getUTCDate();
const year = date.getUTCFullYear();
const hours = d.getUTCHours() > 12 ? d.getUTCHours() - 12 : d.getUTCHours();
const minutes = d.getUTCMinutes();

return `${month}-${day}-${year} ${hours}:${minutes}`}
Scott
  • 63
  • 7