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 203 views
2 answers 350 views
1 answer 233 views
1 answer 230 views
1 answer 283 views
...