javascript convert date to string format dd/mm/yyyy code example
Example 1: javascript date today dd mm yyyy
var today = new Date();
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
today = mm + '/' + dd + '/' + yyyy;
document.write(today);
Example 2: javascript format date to dd-mm-yyyy
function pad2(n) {
return (n < 10 ? '0' : '') + n;
}
var date = new Date();
var month = pad2(date.getMonth()+1);
var day = pad2(date.getDate());
var year= date.getFullYear();
var formattedDate = day+"-"+month+"-"+year;
alert(formattedDate);
Example 3: javascript date to string format dd mmm yyyy
Date.prototype.toShortFormat = function() {
let monthNames =["Jan","Feb","Mar","Apr",
"May","Jun","Jul","Aug",
"Sep", "Oct","Nov","Dec"];
let day = this.getDate();
let monthIndex = this.getMonth();
let monthName = monthNames[monthIndex];
let year = this.getFullYear();
return `${day}-${monthName}-${year}`;
}
let anyDate = new Date(1528578000000);
console.log(anyDate.toShortFormat());