Last active
December 15, 2020 14:32
-
-
Save davidalves1/9e3853753320daa098f8f044f49a85bf to your computer and use it in GitHub Desktop.
Convert Date to YYYY-MM-DD in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param date Date Object | |
* @return String | |
*/ | |
function convertDate(date) { | |
const dateStr = date.toLocaleDateString('pt-BR'); | |
return dateStr.split('/').reverse().join('-'); | |
} | |
// Example | |
const d = new Date(); | |
console.log(convertDate(d)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment