1var today = new Date();
2var dd = String(today.getDate()).padStart(2, '0');
3var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
4var yyyy = today.getFullYear();
5
6today = mm + '/' + dd + '/' + yyyy;
7document.write(today);
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
1 dateString = '2020-04-03 20:30:55';
2
3function formatDate(dateString)
4 {
5 var allDate = dateString.split(' ');
6 var thisDate = allDate[0].split('-');
7 var thisTime = allDate[1].split(':');
8 var newDate = [thisDate[2],thisDate[1],thisDate[0] ].join("-");
9
10 var suffix = thisTime[0] >= 12 ? "PM":"AM";
11 var hour = thisTime[0] > 12 ? thisTime[0] - 12 : thisTime[0];
12 var hour =hour < 10 ? "0" + hour : hour;
13 var min = thisTime[1] ;
14 var sec = thisTime[2] ;
15 var newTime = hour + ':' + min + suffix;
16
17 return newDate + ' ' + newTime;
18 }