Home > database >  Change Date format Inside span tag using javascript
Change Date format Inside span tag using javascript

Time:08-18

I have a date inside span and I want to change its format. Ex. from "August 14, 2022 6:36 am" to "8/14/2022" How to do this using javascript?

<span >August 18, 2022 10:08 am</span>

CodePudding user response:

Make use of Date:

const spanElement = document.querySelector(".question-date")
const currentDate = new Date(spanElement.innerHTML)
spanElement.innerHTML = currentDate.toLocaleDateString("en-US")

CodePudding user response:

You can use this function:

function convertDate(date_string) {
  let a = new Date(date_string);
  let b = a.toLocaleDateString().split("/");
  return b[1]   "/"   b[0]   "/"   b[2]
}

console.log(convertDate("August 14, 2022 6:36 am"))

  • Related