PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 18, 2022

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

 February 18, 2022     ajax, javascript, phpmyadmin     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing