.Maui Blog

Javascript – Current date with format yyyy-mm-dd hh:MM:ss

function NOW() {

    var date = new Date();
    var aaaa = date.getFullYear();
    var gg = date.getDate();
    var mm = (date.getMonth() + 1);

    if (gg < 10)
        gg = "0" + gg;

    if (mm < 10)
        mm = "0" + mm;

    var cur_day = aaaa + "-" + mm + "-" + gg;

    var hours = date.getHours()
    var minutes = date.getMinutes()
    var seconds = date.getSeconds();

    if (hours < 10)
        hours = "0" + hours;

    if (minutes < 10)
        minutes = "0" + minutes;

    if (seconds < 10)
        seconds = "0" + seconds;

    return cur_day + " " + hours + ":" + minutes + ":" + seconds;

}

NOW(); // "2017-10-17 14:02:33"

You can include this feature on your web pages with our free CDN:

<script src="https://cdn.dotmaui.com/dotmaui/js/now.min.js"></script>
Exit mobile version