How to convert DD-MM-YYYY to YYYY-MM-DD format in JavaScript

2 Answers

0 votes
let date = '16-03-2022';

const [day, month, year] = date.split('-');

date = [year, month, day].join('-');

console.log(date); 

  
  
  
  
/*
run:
  
"2022-03-16"
  
*/

 



answered Mar 16, 2022 by avibootz
0 votes
let date = '16-03-2022';

date = date.split("-").reverse().join("-");

console.log(date); 

  
  
  
  
/*
run:
  
"2022-03-16"
  
*/

 



answered Mar 16, 2022 by avibootz

Related questions

1 answer 122 views
2 answers 231 views
1 answer 144 views
1 answer 137 views
1 answer 174 views
...