I have written a javascript function to solve this problem. Here I get the current date-time using the new Date() object. Then formate it.
Get Current Date Function:
function getCurrentDate(formate='mm/dd/yyyy'){
var today = new Date();
var currentDate = {};
currentDate['dd'] = String(today.getDate()).padStart(2, '0');
currentDate['mm'] = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
currentDate['yyyy'] = today.getFullYear();
today = formate.replace("mm", currentDate['mm']);
today = today.replace("dd", currentDate['dd']);
today = today.replace("yyyy", currentDate['yyyy']);
return today;
}
Outputs:
console.log(getCurrentDate());
08/20/2021
console.log(getCurrentDate('dd/mm/yyyy'));
20/08/2021
console.log(getCurrentDate('dd-mm-yyyy'));
20-08-2021
console.log(getCurrentDate('mm-dd-yyyy'));
08-20-2021
console.log(getCurrentDate('mm-yyyy'));
08-2021
Source: Tech Tutorial