Friday, February 18, 2022

[FIXED] How to format date that is being pulled from a database?

Issue

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!


Solution

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>
      ...


Answered By - Paul Shryock

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.