-2

This is my code. I am using it in my program to calculate the time difference between this date and the current date so I need to use the new Date() function but I don't want it to display the GMT on the website. I just want it to display the date of December 23, 2020.

var createdDate = new Date("December 23, 2020");
document.getElementById("dateCreated").innerHTML = "Date created: " +
  "<br />" + createdDate
<span id="dateCreated"></span>
terrymorse
  • 6,771
  • 1
  • 21
  • 27

1 Answers1

1

Use .toLocalDateString() with the following format options:

const createdDate = new Date("December 23, 2020");
const formattedDate = createdDate.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' });
document.getElementById("dateCreated").innerHTML = "Date created: " +
  "<br />" + formattedDate
<span id="dateCreated"></span>
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31