0

I am trying to format a date that is being requested from my database. I looked up how to format date and I got this as a solution:

var d = new Date("2015/03/25");

Which would work, but my date is stored in a data object and it's a different date for every row (since the data from the DB is being displayed in a table.

let DateSubmitted = new Date(**data[i].DateSubmitted)**;
    list += `
    <tr>
      <td class="requestID">${data[i].requestID}</td>
      <td class="date-requested">**${DateSubmitted}**</td>
      <td class="item-code">${data[i].itemCode}</td>
      ...

My question is, how do I format each date returned from data[i]?

Thanks!

Kenny S
  • 35
  • 4
  • 1
    You didn't tell us what kind of database this is. You didn't tell us how you are retrieving the data. You tagged this as "javascript" but your code looks like php. Did you really mean to say "my date is stored in a data object"? You've tagged this with phpmyadmin which suggests it might be a MySQL instance - but that is a relational database, not an object database. You haven't said how you want the value formatted. – symcbean Sep 05 '19 at 21:32
  • This is a duplicate of https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date – Paul Shryock Sep 05 '19 at 21:32
  • 1
    Why does it matter that it comes from the database? (hint: it doesn’t, only the immediate value is of interest) – user2864740 Sep 05 '19 at 21:42

1 Answers1

1

ES5:

var dateFormatted = DateSubmitted.getDate() + '/' + (DateSubmitted.getMonth() + 1) + '/' + DateSubmitted.getFullYear();

ES6:

const dateFormatted = `${DateSubmitted.getDate()}/${DateSubmitted.getMonth() + 1}/${DateSubmitted.getFullYear()}`

So,

let DateSubmitted = new Date(**data[i].DateSubmitted)**;
const dateFormatted = `${DateSubmitted.getDate()}/${DateSubmitted.getMonth() + 1}/${DateSubmitted.getFullYear()}`

    list += `
    <tr>
      <td class="requestID">${data[i].requestID}</td>
      <td class="date-requested">**${dateFormatted}**</td>
      <td class="item-code">${data[i].itemCode}</td>
      ...
Paul Shryock
  • 826
  • 7
  • 15