how to get formatted date time like 2009-05-29 21:55:57 using javascript?

Although it doesn’t pad to two characters in some of the cases, it does what I expect you want

function getFormattedDate() {
    var date = new Date();
    var str = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + " " +  date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();

    return str;
}

Leave a Comment