javascript convert datetime to string format dd/mm/yyyy code example
Example 1: javascript convert date to yyyy-mm-dd
const formatYmd = date => date.toISOString().slice(0, 10);
formatYmd(new Date());
Example 2: js date format yyyy-mm-dd
yourDate.toISOString().split('T')[0]
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());