javascript date tostring format yyyy mm dd hh mm ss code example
Example 1: 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 2: java script print date in YYYY-MM-DD HH:MM:SS format
let date_ob = new Date();
let date = ("0" + date_ob.getDate()).slice(-2);
let month = ("0" + (date_ob.getMonth() + 1)).slice(-2);
let year = date_ob.getFullYear();
let hours = date_ob.getHours();
let minutes = date_ob.getMinutes();
let seconds = date_ob.getSeconds();
console.log(year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds);
Example 3: javascript date format dd-mm-yyyy
var date_format = new Date();
document.write(innerHTML = date_format.getMonth()+'-'+ date_format.getDate()+'-'+date_format.getFullYear());