1

By taking data from Wordpress API I'm getting date/time this way - 2019-11-29 19:00:00

How to modify it by making look like this - November 29, 2019 19:00

HTML:

<p class="date">DATE</p>

JS:

const date = postCopy.querySelector(".date");
date.textContent = post.event_date
Aivars
  • 43
  • 5

1 Answers1

0

I suggest using the toLocaleDateString() method available in vanilla JS.

Relevant doc: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString

let initialDate = "2019-11-29 19:00:00"

let formattedDate = new Date(initialDate).toLocaleDateString('en-US', {
  year: 'numeric',
  month: 'long',
  day: '2-digit',
  hour: "2-digit",
  minute: "2-digit",
  hour12: false

})
console.log(formattedDate)

If you don't like this approach, take a look at the 51 solutions proposed in this similar question: How to format a JavaScript date

Félix Paradis
  • 5,165
  • 6
  • 40
  • 49