Sometimes, we want to convert a date in dd/mm/yyyy format to mm/dd/yyyy format in JavaScript.
In this article, we’ll look at how to convert a date in dd/mm/yyyy format to mm/dd/yyyy format in JavaScript.
How to convert a date in dd/mm/yyyy format to mm/dd/yyyy format in JavaScript?
To convert a date in dd/mm/yyyy format to mm/dd/yyyy format in JavaScript, we can split the date string and combine the parts.
For instance, we write:
const date = "24/09/2022";
const [day, month, year] = date.split("/");
const newDate = `${month}/${day}/${year}`
console.log(newDate)
We call date.split
with '/'
to split the date string by the slashes.
Then we combine the parts that we destructured from the array with ${month}/${day}/${year}
.
Therefore, newDate
is "09/24/2022"
.
Conclusion
To convert a date in dd/mm/yyyy format to mm/dd/yyyy format in JavaScript, we can split the date string and combine the parts.