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>

2 Comments

  1. I uploaded this to my website but it didn’t work.
    time.html create and copy in public.html file

    http://www.mywebsite/time.html

    time.html here:

    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;

    }

    but not working. why?
    thank you very much

Leave a Comment

Your email address will not be published. Required fields are marked *