how to convert date object to dd mm yyyy format in javascript code example
Example 1: javascript convert date to yyyy-mm-dd
const formatYmd = date => date.toISOString().slice(0, 10);
formatYmd(new Date());
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: change the way Date.now().toString() is logged
var date = Date.parse('Sun May 11,2014');
function format(date) {
date = new Date(date);
var day = ('0' + date.getDate()).slice(-2);
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var year = date.getFullYear();
return year + '-' + month + '-' + day;
}
console.log(format(date));