-2

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.

user1724708
  • 1,409
  • 7
  • 31
  • 53
  • Does this answer your question? [How to format a JavaScript date](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date) – devlin carnate Dec 02 '21 at 21:29
  • Does this answer your question? [convert dd/mm/yyyy to mm/dd/yyyy in javascript](https://stackoverflow.com/questions/5433313/convert-dd-mm-yyyy-to-mm-dd-yyyy-in-javascript) – Daniel Beck Dec 02 '21 at 21:30

1 Answers1

0

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)
callmenikk
  • 1,358
  • 2
  • 9
  • 24