1// `date` is a `Date` object
2const formatYmd = date => date.toISOString().slice(0, 10);
3
4// Example
5formatYmd(new Date()); // 2020-05-06
1function pad2(n) {
2 return (n < 10 ? '0' : '') + n;
3}
4
5var date = new Date();
6var month = pad2(date.getMonth()+1);//months (0-11)
7var day = pad2(date.getDate());//day (1-31)
8var year= date.getFullYear();
9
10var formattedDate = day+"-"+month+"-"+year;
11alert(formattedDate); //28-02-2021