Search code examples
javascriptdate-format

How to convert static / constant Date to specific format using Javascript


I have this basic javascript that outputs a constant / static date to the console:

var TodaysDate ="12/02/2021"; console.log(TodaysDate);

The output is simply: 12/02/2021

... Is there a way to get the date to output in the following format: YYYY-MM-DD, without changing the original variable text in the " " to 2021/12/02? Thanks in advance for any assistance provided.


Solution

  • You can use simple ES6 methods to easily reverse your string

    const TodaysDate ="12/02/2021"
    const reverseDate = TodaysDate.split("/").reverse().join("/")
    
    console.log(reverseDate)