Home > database >  How to convert static / constant Date to specific format using Javascript
How to convert static / constant Date to specific format using Javascript

Time:12-03

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.

CodePudding user response:

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)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related